
Binary Search
Binary search is a technique that lets a computer find a target value in a sorted list extremely quickly by halving the list at every step. Instead of checking every entry one after another, binary search eliminates half of the remaining possibilities with each comparison.
Binary search is a method for finding a specific value in a sorted list. Instead of counting through from the beginning, you first look at the middle. If the target value is smaller than the middle element, you only keep searching in the left half. If it is larger, only in the right half. You repeat this step until the value is found or it is established that it does not occur. With each step, the set of still-possible matches is halved.
Why halving is so powerful
The decisive advantage lies in speed. A simple search that checks every entry one by one needs, in the worst case, as many steps as the list is long. With a million entries, that would be up to a million comparisons.
Binary search needs at most 20 steps for this. That is because of how quickly halving takes effect: 2 to the power of 20 already exceeds a million. If you double the length of the list, only a single additional step is needed. Computer scientists call this growth behavior “logarithmic” — the effort does not grow with the amount of data, but only with its base-2 logarithm.
There is, however, one condition: the list must be sorted. If it is not, the method does not work, because then you cannot decide which half to keep searching in. This caveat is no drawback in everyday life, but it explains why sorting data receives so much attention in computer science.
Step by step through a search
Suppose you are looking for the number 73 in the sorted list [10, 24, 37, 51, 68, 73, 89, 95]. The list has 8 entries, the middle element is 51. Since 73 is larger than 51, the left half is immediately ruled out. What remains is [68, 73, 89, 95].
In this remainder, the middle element is 89. Since 73 is smaller, the right half is ruled out. Now [68, 73] remains. Middle element: 68. Since 73 is larger, only 73 itself remains in the end — found, after just three comparisons. A linear search would have gone through all eight elements in the worst case.
The procedure can be written either as a loop or as a function that calls itself — the latter is called recursion. Both variants are common, and most programming languages come with a ready-made implementation.
Where binary search hides without you noticing
Binary search is one of the most widely used fundamental techniques in software development. Databases employ it when searching for a record by a key. Git, the version control system used by almost all software developers, uses binary search in the “git bisect” command: it is used to find, in a long sequence of versions, the one in which a bug first appeared — by halving instead of scanning through.
The principle also appears in AI systems. When a model optimizes certain thresholds or parameters during training, search routines running in the background can rely on binary search to efficiently narrow down the correct value.
A common misconception is confusing it with the term “binary” in the sense of ones and zeros. Binary search has nothing to do with that. Here, “binary” comes from the Latin “binarius” and simply means “consisting of two parts” — in this case, the two halves into which the list is split at each step.