In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. 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. Binary search runs in logarithmic time in the worst case, making O(logn) 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.
Set L to 0 and R to n−1. If L>R, the search terminates as unsuccessful. 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. If Am<T, set L to m+1 and go to step 2. If Am>T, set R to m−1 and go to step 2. 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.
function binary_search(A, n, T) is L := 0 R := n − 1 while L ≤ R do m := L + floor((R - L) / 2) if A[m] < T then L := m + 1 else if A[m] > T then R := m − 1 else: return m return 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.
Set L to 0 and R to n−1. While L≠R, 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. If Am>T, set R to m−1. Else, Am≤T; set L to m. 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:
function binary_search_alternative(A, n, T) is L := 0 R := n − 1 while L != R do m := L + ceil((R - L) / 2) if A[m] > T then R := m − 1 else: L := m if A[L] = T then return L return 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:
Set L to 0 and R to n. While L<R, 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. If Am<T, set L to m+1. Else, Am≥T; set R to m. 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:
function binary_search_leftmost(A, n, T): L := 0 R := n while L < R: m := L + floor((R - L) / 2) if A[m] < T: L := m + 1 else: R := m return L
Procedure for finding the rightmost element To find the rightmost element, the following procedure can be used:
… excerpt ends here. Continue reading the full article.




![Binary search: A tree representing binary search. The array being searched here is [20,30,40,50,80,90,100], and the target value is 40.](https://thumb.wikimedia.org/wikipedia/commons/thumb/f/f4/Binary_search_example_tree.svg/500px-Binary_search_example_tree.svg.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)
