language-icon Old Web
English
Sign In

Binary search algorithm

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 ( log ⁡ n ) {displaystyle O(log n)} comparisons, where n {displaystyle n} is the number of elements in the array, the O {displaystyle O} is Big O notation, and log {displaystyle log } is the logarithm. 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. 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. Given an array A {displaystyle A} of n {displaystyle n} elements with values or records A 0 , A 1 , A 2 , … , A n − 1 {displaystyle A_{0},A_{1},A_{2},ldots ,A_{n-1}} sorted such that A 0 ≤ A 1 ≤ A 2 ≤ ⋯ ≤ A n − 1 {displaystyle A_{0}leq A_{1}leq A_{2}leq cdots leq A_{n-1}} , and target value T {displaystyle T} , the following subroutine uses binary search to find the index of T {displaystyle T} in A {displaystyle A} . This iterative procedure keeps track of the search boundaries with the two variables L {displaystyle L} and R {displaystyle 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. Alternatively, the algorithm may take the ceiling of L + R 2 {displaystyle {frac {L+R}{2}}} , or the least integer greater than or equal to L + R 2 {displaystyle {frac {L+R}{2}}} . This may change the result if the target value appears more than once in the array.

[ "Search algorithm", "Jump search" ]
Parent Topic
Child Topic
    No Parent Topic