### Data Structures ### Topic: Binary Search for Sorted Arrays ### 1\. Introduction to Binary Search Binary Search is an efficient searching algorithm used to find a specific element in a sorted array. It works by repeatedly dividing the array into two halves. Instead of checking every element one by one, it directly searches in the half where the required element may exist. ### 2\. What is a Sorted Array? A sorted array is a collection of elements arranged in a specific order, either ascending or descending. Example: `[10, 20, 30, 40, 50, 60, 70]` In this array, all elements are arranged in ascending order. Binary Search requires a sorted array to work correctly. ### 3\. Definition of Binary Search Binary Search is a divide-and-conquer algorithm that repeatedly compares the target element with the middle element of a sorted array. * If the target matches the middle element, the search is successful. * If the target is smaller, the search continues in the left half. * If the target is greater, the search continues in the right half. This process continues until the element is found or the search space becomes empty. ### 4\. Working of Binary Search Binary Search follows these basic steps: * Start with the first and last index of the array. * Calculate the middle index. * Compare the middle element with the target value. * If both are equal, the element is found. * If the target is smaller, search the left half. * If the target is greater, search the right half. * Repeat the process until the element is found or no elements remain. ### 5\. Example of Binary Search Suppose we have the following sorted array: `[10, 20, 30, 40, 50, 60, 70]` We want to search for the number 50. Step 1: The middle element is 40. Step 2: Since 50 is greater than 40, we search the right half. Step 3: The new middle element is 60. Step 4: Since 50 is smaller than 60, we search the left half. Step 5: We find 50 successfully. Result: The required element is found in the array. ### 6\. Binary Search Algorithm (Pseudocode) ```text BinarySearch(array, target) low = 0 high = length(array) - 1 while low <= high: mid = (low + high) // 2 if array[mid] == target: return mid else if array[mid] < target: low = mid + 1 else: high = mid - 1 return -1 ``` This algorithm returns the index of the target element if it exists. Otherwise, it returns -1. ### 7\. Time and Space Complexity Binary Search is considered a very fast searching algorithm. Time Complexity: * Best Case: O(1) – When the middle element is the target. * Average Case: O(log n) – When the search requires multiple divisions. * Worst Case: O(log n) – When the algorithm repeatedly divides the array until the element is found or the search ends. Space Complexity: \- O(1) for iterative Binary Search because it uses a fixed amount of extra memory. ### 8\. Difference Between Binary Search and Linear Search Binary Search: * Requires a sort
### Data Structures
### Topic: Binary Search for Sorted Arrays
### 1\. Introduction to Binary Search
Binary Search is an efficient searching algorithm used to find a specific element in a sorted array. It works by repeatedly dividing the array into two halves. Instead of checking every element one by one, it directly searches in the half where the required element may exist.
### 2\. What is a Sorted Array?
A sorted array is a collection of elements arranged in a specific order, either ascending or descending.
Example:
`[10, 20, 30, 40, 50, 60, 70]`
In this array, all elements are arranged in ascending order. Binary Search requires a sorted array to work correctly.
### 3\. Definition of Binary Search
Binary Search is a divide-and-conquer algorithm that repeatedly compares the target element with the middle element of a sorted array.
* If the target matches the middle element, the search is successful.
* If the target is smaller, the search continues in the left half.
* If the target is greater, the search continues in the right half.
This process continues until the element is found or the search space becomes empty.
### 4\. Working of Binary Search
Binary Search follows these basic steps:
* Start with the first and last index of the array.
* Calculate the middle index.
* Compare the middle element with the target value.
* If both are equal, the element is found.
* If the target is smaller, search the left half.
* If the target is greater, search the right half.
* Repeat the process until the element is found or no elements remain.
### 5\. Example of Binary Search
Suppose we have the following sorted array:
`[10, 20, 30, 40, 50, 60, 70]`
We want to search for the number 50.
Step 1: The middle element is 40.
Step 2: Since 50 is greater than 40, we search the right half.
Step 3: The new middle element is 60.
Step 4: Since 50 is smaller than 60, we search the left half.
Step 5: We find 50 successfully.
Result: The required element is found in the array.
### 6\. Binary Search Algorithm (Pseudocode)
```text
BinarySearch(array, target)
low = 0
high = length(array) - 1
while low <= high:
mid = (low + high) // 2
if array[mid] == target:
return mid
else if array[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
```
This algorithm returns the index of the target element if it exists. Otherwise, it returns -1.
### 7\. Time and Space Complexity
Binary Search is considered a very fast searching algorithm.
Time Complexity:
* Best Case: O(1) – When the middle element is the target.
* Average Case: O(log n) – When the search requires multiple divisions.
* Worst Case: O(log n) – When the algorithm repeatedly divides the array until the element is found or the search ends.
Space Complexity:
\- O(1) for iterative Binary Search because it uses a fixed amount of extra memory.
### 8\. Difference Between Binary Search and Linear Search
Binary Search:
* Requires a sort
Created using ChatSlide
Binary search is a powerful algorithm that relies on sorted data to efficiently locate a target value. By comparing the target with the middle element of the array, it systematically eliminates half of the candidates with each comparison. For instance, to find 50 in a sorted array, the search narrows down by adjusting the low and high indices based on comparisons. This method boasts a time complexity of O(log n) and is ideal for large datasets, outperforming linear search in terms of efficiency.