Binary Search Algorithm - David-Chae/Algorithms_Notes_Solutions GitHub Wiki
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
Binary Search Algorithm:
The basic steps to perform Binary Search are:
- Begin with the mid element of the whole array as a search key.
- If the value of the search key is equal to the item then return an index of the search key.
- Or if the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
- Otherwise, narrow it to the upper half.
- Repeatedly check from the second point until the value is found or the interval is empty.
Binary Search Algorithm can be implemented in the following two ways
- Iterative Method
- Recursive Method
-
Iteration Method
binarySearch(arr, x, low, high) repeat till low = high mid = (low + high)/2 if (x == arr[mid]) return mid
else if (x > arr[mid]) // x is on the right side low = mid + 1 else // x is on the left side high = mid - 1
-
Recursive Method (The recursive method follows the divide and conquers approach)
binarySearch(arr, x, low, high) if low > high return False
else mid = (low + high) / 2 if x == arr[mid] return mid else if x > arr[mid] // x is on the right side return binarySearch(arr, x, mid + 1, high) else // x is on the right side return binarySearch(arr, x, low, mid - 1)
Step-by-step Binary Search Algorithm: We basically ignore half of the elements just after one comparison.
Compare x with the middle element. If x matches with the middle element, we return the mid index. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half. Else (x is smaller) recur for the left half.
Recursive implementation of Binary Search in Java
Recursive implementation of Binary Search in Python
Time Complexity: O(log n) Auxiliary Space: O(log n)
Iterative implementation of Binary Search in Java
Iterative Implementation of Binary Search in Python
Time Complexity: O(log n) Auxiliary Space: O(1)
Algorithmic Paradigm: Decrease and Conquer.
Note: Here we are using "int mid = low + (high β low)/2;"
Maybe, you wonder why we are calculating the middle index this way, we can simply add the lower and higher index and divide it by 2.
int mid = (low + high)/2;
But if we calculate the middle index like this means our code is not 100% correct, it contains bugs.
That is, it fails for larger values of int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value(231 β 1 ).
The sum overflows to a negative value and the value stays negative when divided by 2. In java, it throws ArrayIndexOutOfBoundException.
int mid = low + (high β low)/2;
So itβs better to use it like this. This bug applies equally to merge sort and other divide and conquer algorithms.
Reference: https://www.geeksforgeeks.org/binary-search/?ref=lbp