Also known as logarithmic search or half-interval search.
Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.
BS runs in
There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search.
However, binary search can be used to solve a wider range of problems, such as:
- Finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array.
- Finding the smallest and largest element.
- Fractional cascading speeds up binary searches for the same value in multiple arrays. It efficiently solves a number of search problems in computational geometry.
- _Exponential search) extends binary search to unbounded lists.
- Binary search tree and B-tree data sturctures are based on binary search.