- Home
- Algorithms
- Binary Search Algorithm

Binary search is a highly efficient search algorithm used to find a target element within a sorted collection of data. It employs a divide-and-conquer strategy, repeatedly halving the search space until the target is located or it determines that the target is not present in the data. This algorithm drastically reduces the number of comparisons required, making it much faster than linear search for large datasets.

Advertise on this site. I promise you will like the rates :)

Binary search operates by dividing the sorted data into two halves and comparing the middle element with the target. Based on this comparison, the algorithm decides which half of the data contains the target. This process is repeated until the target is found or the search space is exhausted.

- Start with the entire sorted data.
- Compare the middle element with the target.
- If the middle element equals the target, the search is successful, and the algorithm terminates.
- If the middle element is greater than the target, discard the right half of the data and repeat step 2 in the left half.
- If the middle element is less than the target, discard the left half of the data and repeat step 2 in the right half.
- Repeat steps 2-5 until the target element is found or the search space is empty.

Binary search is highly efficient for large datasets and scenarios where the data is sorted. It is particularly useful when:

- The dataset is sorted: Binary search requires a sorted collection, allowing it to exploit the sorted order for efficient searching.
- The data is large: Binary search reduces the search space by half with each step, significantly cutting down the number of comparisons needed.
- The data is static: Binary search works best when the data remains unchanged or changes infrequently since re-sorting is required if the data changes.

The time complexity of binary search is **O(log n)**, where "n" is the
number of elements in the collection. With each step, the search space
is halved, leading to a dramatic reduction in the number of comparisons
required. This logarithmic growth makes binary search significantly more
efficient than linear search (O(n)) for large datasets.

Let's consider an example of searching for a specific number, say 7, in a sorted array: [1, 2, 3, 4, 5, 6, 7, 8, 9]. Binary search starts by comparing the middle element (5) with the target (7). Since 7 is greater than 5, the algorithm discards the left half of the array and searches in the right half. It compares the middle element of the right half (7) with the target and finds a match, concluding that the target (7) is present in the array.

Here's an example of an ANSI C function that implements the binary search algorithm to find a target element within a sorted array:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <stdio.h> // Function to perform binary search int binarySearch(int arr[], int left, int right, int target) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; // Return the index of the target element if found } if (arr[mid] < target) { left = mid + 1; // Search the right half } else { right = mid - 1; // Search the left half } } return -1; // Return -1 if the target element is not found } int main() { int arr[] = {1, 2, 3, 5, 7, 8, 9}; int size = sizeof(arr) / sizeof(arr[0]); int target = 7; int index = binarySearch(arr, 0, size - 1, target); if (index != -1) { printf("Target element %d found at index %d\n", target, index); } else { printf("Target element %d not found in the array\n", target); } return 0; } |

Binary search is a powerful search algorithm well-suited for sorted datasets. Its efficient divide-and-conquer approach allows it to quickly locate target elements, making it an essential tool for various applications, such as efficient data retrieval, database indexing, and more. By leveraging the sorted nature of the data, binary search exemplifies the effectiveness of algorithmic strategies in optimizing search operations.

- Home
- Algorithms
- Binary Search Algorithm

Search this site: