What is the binary search algorithm?
Binary Search Algorithm is used to find a certain value of x for which a certain defined function f(x) needs to be maximized or minimized. It is frequently used to search an element in a sorted sequence by repeatedly dividing the search interval into halves. Begin with an interval covering the whole sequence, if the value of the search key is less than the item in the middle of the interval, then search in the left half of the interval otherwise search in the right half. Repeatedly check until the value is found or the interval is empty.
The main condition to perform a Binary Search is that the sequence must be monotonous i.e., it must be either increasing or decreasing.
A function f(x) is said to be monotonic if and only if for any x if f(x) returns true, then for any value of y (where y > x) should also return true and similarly if for a certain value of x for which f(x) is false, then for any value z (z < x) the function should also return false.
How to solve a question using Binary Search:
If the function is
And the task is to find the maximum value of x such that f(x) is less than or equal to the target value. The interval in which we will search the target value for the given
is from 0 to target value.
Then we can use binary search for this problem since the function
is a monotonically increasing function.
Target = 17
f(x) = x2, since the function is monotonic binary search can be applied to it.
Range of search interval will be [0, target]
low = 0, high = 17, calculate mid = (low + high)/2 = 8
Calculate f(8) = 64 which is more than target, so it will return false and high will be updated as high = mid – 1 = 7.
low = 0, high = 7, calculate mid = (low + high)/2 = 3
Calculate f(3) = 9 which is less than target, so it will return true and low will be updated as low = mid + 1 = 4.
low = 4, high = 7, calculate mid = (low + high)/2 = 5
Calculate f(5) = 25 which is more than target, so it will return false and high will be updated as high = mid – 1 = 4.
Now since the range [low, high] converges to a single point i.e 4 so the final result is found, since f(4) = 16 which is the maximum value of the given function less than target.
Below is the implementation of the above example:
The above binary search algorithm requires at most O(log N) comparisons to find the maximum value less than or equal to the target value. And the value of the function f(x) = x2 doesn’t need to be evaluated many times.
Time Complexity: O(logN)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Meta Binary Search | One-Sided Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Sublist Search (Search a linked list in another list)
- Best First Search (Informed Search)
- Repeatedly search an element by doubling it after every successful search
- The Ubiquitous Binary Search | Set 1
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- A Problem in Many Binary Search Implementations
- Longest Common Prefix using Binary Search
- Finding minimum vertex cover size of a graph using binary search
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Binary Search
- Binary Search for Rational Numbers without using floating point arithmetic
- Binary Search in C++ Standard Template Library (STL)
- Randomized Binary Search Algorithm
- Leaf nodes from Preorder of a Binary Search Tree
- C Program for Binary Search (Recursive and Iterative)
- Binary Search using pthread
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.