Lably
Skip to content

Binary search

Search algorithm used in sorted arrays

  • 28 min read
  • 7 chapters
  • 2 labs on Lably

Learn it on Lably

Binary search runs in logarithmic time in the worst case, making O(log⁡n) comparisons, where n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. 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.

There are numerous variations of binary search. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays. Fractional cascading efficiently solves a number of search problems in computational geometry and in numerous other fields. Exponential search extends binary search to unbounded lists. The binary search tree and B-tree data structures are based on binary search.

Algorithm

Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned. If the target value is less than the element, the search continues in the lower half of the array. If the target value is greater than the element, the search continues in the upper half of the array. By doing this, the algorithm eliminates the half in which the target value cannot lie in each iteration.

Procedure

Given an array A of n elements with values or records A0,A1,A2,…,An−1sorted such that A0≤A1≤A2≤⋯≤An−1, and target value T, the following subroutine uses binary search to find the index of T in A.

  1. Set L to 0 and R to n−1.
  2. If L>R, the search terminates as unsuccessful.
  3. Set m (the position of the middle element) to L plus the floor of R−L2, which is the greatest integer less than or equal to R−L2.
  4. If Am<T, set L to m+1 and go to step 2.
  5. If Am>T, set R to m−1 and go to step 2.
  6. Now Am=T, the search is done; return m.

This iterative procedure keeps track of the search boundaries with the two variables L and R. The procedure may be expressed in pseudocode as follows, where the variable names and types remain the same as above, floor is the floor function, and unsuccessful refers to a specific value that conveys the failure of the search.

<b id="mwfg">function</b> binary_search(A, n, T) <b id="mwfw">is</b> L := 0 R := n <span typeof="mw:Entity" id="mwgA">−</span> 1 <b id="mwgQ">while</b> L ≤ R <b id="mwgg">do</b> m := L + floor((R - L) / 2) <b id="mwgw">if</b> A[m] < T <b id="mwhA">then</b> L := m + 1 <b id="mwhQ">else if</b> A[m] > T <b id="mwhg">then</b> R := m <span typeof="mw:Entity" id="mwhw">−</span> 1 <b id="mwiA">else</b>: <b id="mwiQ">return</b> m <b id="mwig">return</b> unsuccessful

Alternatively, the algorithm may take the ceiling of R−L2. This may change the result if the target value appears more than once in the array.

Alternative procedure

In the above procedure, the algorithm checks whether the middle element (m) is equal to the target (T) in every iteration. Some implementations leave out this check during each iteration. The algorithm would perform this check only when one element is left (when L=R). This results in a faster comparison loop, as one comparison is eliminated per iteration, while it requires only one more iteration on average.

Hermann Bottenbruch published the first implementation to leave out this check in 1962.

  1. Set L to 0 and R to n−1.
  2. While L≠R,
    1. Set m (the position of the middle element) to L plus the ceiling of R−L2, which is the least integer greater than or equal to R−L2.
    2. If Am>T, set R to m−1.
    3. Else, Am≤T; set L to m.
  3. Now L=R, the search is done. If AL=T, return L. Otherwise, the search terminates as unsuccessful.

Where ceil is the ceiling function, the pseudocode for this version is:

<b id="mwvw">function</b> binary_search_alternative(A, n, T) <b id="mwwA">is</b> L := 0 R := n <span typeof="mw:Entity" id="mwwQ">−</span> 1 <b id="mwwg">while</b> L != R <b id="mwww">do</b> m := L + ceil((R - L) / 2) <b id="mwxA">if</b> A[m] > T <b id="mwxQ">then</b> R := m <span typeof="mw:Entity" id="mwxg">−</span> 1 <b id="mwxw">else</b>: L := m <b id="mwyA">if</b> A[L] = T <b id="mwyQ">then</b> <b id="mwyg">return</b> L <b id="mwyw">return</b> unsuccessful

Duplicate elements

The procedure may return any index whose element is equal to the target value, even if there are duplicate elements in the array. For example, if the array to be searched was [1,2,3,4,4,5,6,7] and the target was 4, then it would be correct for the algorithm to either return the 4th (index 3) or 5th (index 4) element. The regular procedure would return the 4th element (index 3) in this case. It does not always return the first duplicate (consider [1,2,4,4,4,5,6,7] which still returns the 4th element). However, it is sometimes necessary to find the leftmost element or the rightmost element for a target value that is duplicated in the array. In the above example, the 4th element is the leftmost element of the value 4, while the 5th element is the rightmost element of the value 4. The alternative procedure above will always return the index of the rightmost element if such an element exists.

Procedure for finding the leftmost element

To find the leftmost element, the following procedure can be used:

  1. Set L to 0 and R to n.
  2. While L<R,
    1. Set m (the position of the middle element) to L plus the floor of R−L2, which is the greatest integer less than or equal to R−L2.
    2. If Am<T, set L to m+1.
    3. Else, Am≥T; set R to m.
  3. Return L.

If L<n and AL=T, then AL is the leftmost element that equals T. Even if T is not in the array, L is the rank of T in the array, or the number of elements in the array that are less than T.

Where floor is the floor function, the pseudocode for this version is:

<b id="mwAQI">function</b> binary_search_leftmost(A, n, T): L := 0 R := n <b id="mwAQM">while</b> L < R: m := L + floor((R - L) / 2) <b id="mwAQQ">if</b> A[m] < T: L := m + 1 <b id="mwAQU">else</b>: R := m <b id="mwAQY">return</b> L

Procedure for finding the rightmost element

To find the rightmost element, the following procedure can be used:

  1. Set L to 0 and R to n.
  2. While L<R,
    1. Set m (the position of the middle element) to L plus the floor of R−L2, which is the greatest integer less than or equal to R−L2.
    2. If Am>T, set R to m.
    3. Else, Am≤T; set L to m+1.
  3. Return R−1.

If R>0 and AR−1=T, then AR−1 is the rightmost element that equals T. Even if T is not in the array, n−R is the number of elements in the array that are greater than T.

Where floor is the floor function, the pseudocode for this version is:

<b id="mwATQ">function</b> binary_search_rightmost(A, n, T): L := 0 R := n <b id="mwATU">while</b> L < R: m := L + floor((R - L) / 2) <b id="mwATY">if</b> A[m] > T: R := m <b id="mwATc">else</b>: L := m + 1 <b id="mwATg">return</b> R - 1

Approximate matches

The above procedure only performs exact matches, finding the position of a target value. However, it is trivial to extend binary search to perform approximate matches because binary search operates on sorted arrays. For example, binary search can be used to compute, for a given value, its rank (the number of smaller elements), predecessor (next-smallest element), successor (next-largest element), and nearest neighbor. Range queries seeking the number of elements between two values can be performed with two rank queries.

  • Rank queries can be performed with the procedure for finding the leftmost element. The number of elements less than the target value is returned by the procedure.
  • Predecessor queries can be performed with rank queries. If the rank of the target value is r, its predecessor is r−1.
  • For successor queries, the procedure for finding the rightmost element can be used. If the result of running the procedure for the target value is r, then the successor of the target value is r+1.
  • The nearest neighbor of the target value is either its predecessor or successor, whichever is closer.
  • Range queries are also straightforward. Once the ranks of the two values are known, the number of elements greater than or equal to the first value and less than the second is the difference of the two ranks. This count can be adjusted up or down by one according to whether the endpoints of the range should be considered to be part of the range and whether the array contains entries matching those endpoints.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Theoretical computer science