Binary search is an algorithm for quickly finding a target value inside a sorted list by repeatedly checking the middle element and narrowing the search to the half of the list that could still contain the target. Because it eliminates roughly half of the remaining possibilities with every single check, binary search can locate a value inside a list of a million items in about twenty comparisons, compared with up to a million comparisons for a simple linear search that checks every element one by one. This dramatic speed advantage makes binary search one of the first algorithms taught in computer science courses and one of the most widely reused building blocks in real software.
How the Algorithm Works
Binary search begins by comparing the target value with the element sitting exactly in the middle of the sorted list. If the middle element equals the target, the search is finished. If the target is smaller than the middle element, the algorithm discards the entire upper half of the list, since a sorted list guarantees nothing larger than the middle element could be hiding there, and repeats the same middle-comparison process on the remaining lower half. If the target is larger, it discards the lower half instead. This halving repeats until either the target is found or the remaining range shrinks to nothing, meaning the value is not present in the list at all.
This halving behavior is what gives binary search its efficiency, formally described as O(log n) time, meaning the number of comparisons needed grows only with the logarithm of the list’s size rather than the size itself. Doubling the size of the list adds only one extra comparison in the worst case, which is why binary search remains fast even on enormous datasets where a linear scan would become impractically slow. The one essential requirement is that the list must already be sorted; running binary search on unsorted data produces unreliable results, since the halving logic depends entirely on knowing which half a value must fall into.
Variations and Real-World Use
Binary search can be written either recursively, where the function calls itself on the narrowed half of the list each time, or iteratively, using a loop that updates the boundaries of the search range on each pass; both versions are common; the iterative version is often preferred in performance-sensitive code because it avoids the extra memory used by recursive function calls. A closely related family of techniques, sometimes called binary search on the answer, applies the same halving idea not to a literal list but to a range of possible numeric answers, repeatedly guessing a middle value and checking whether it satisfies some condition, which makes the technique useful for a surprisingly wide range of optimization problems beyond simple lookup.
Binary search trees extend the same core idea into a data structure, arranging values so that every node’s left subtree holds smaller values and its right subtree holds larger ones, which lets a search follow the same repeated halving logic by walking down the tree instead of narrowing a flat list. Database indexes, the structures that let a database find a specific row without scanning an entire table, frequently rely on tree structures built on this same principle. Standard library functions in most programming languages, including Python’s bisect module and Java’s built-in binary search methods, provide ready-made binary search implementations so that developers rarely need to write the algorithm from scratch.
Everyday Examples
Binary search has an intuitive everyday analogy in the way most people naturally look up a word in a printed dictionary. Rather than starting at the first page and reading every entry until the right word turns up, a person flips to roughly the middle of the book, checks whether the target word comes before or after that page alphabetically, and then repeats the same halving process on the correct half, quickly narrowing in on the right page in only a handful of flips. Guessing games where one player picks a number and another tries to find it using only yes-or-no feedback about whether each guess is too high or too low follow the identical strategy, and the fastest possible guessing approach is mathematically equivalent to binary search over the range of possible numbers.
Frequently Asked Questions
Why does binary search require a sorted list? The algorithm decides which half of the list to discard by comparing the target to the middle element, a decision that is only valid if every element smaller than the middle is guaranteed to sit to one side and every larger element to the other. An unsorted list offers no such guarantee, so the halving logic breaks down and the result cannot be trusted.
How much faster is binary search than linear search? The difference grows dramatically with list size. Searching a list of one thousand items takes at most ten comparisons with binary search versus up to one thousand with linear search, and a list of one million items takes at most about twenty comparisons with binary search versus up to one million with linear search.
Can binary search be used on data other than numbers? Yes, as long as the data has a consistent ordering, such as alphabetical order for words or chronological order for dates. Binary search only needs a way to compare two elements and decide which one comes first, which is true of far more than just numeric data.
Key facts
- Binary search requires the list to already be sorted.
- Each comparison discards half of the remaining possibilities.
- Its time complexity is O(log n), far faster than linear search for large lists.
- It can be written recursively or iteratively.
- Binary search trees apply the same idea as a data structure.
Quick checkWhy is binary search so much faster than checking every item one by one?Show answer
Binary search eliminates about half of the remaining possibilities with every comparison, so the number of steps needed grows only with the logarithm of the list size, while checking items one by one can require as many steps as there are items.
Sources
- Introduction to Algorithms (Cormen et al.); Python bisect module documentation
Editorially reviewed by Specialty Digest Editorial TeamLast reviewed November 2, 2026Researched and drafted with AI assistanceReport an issue
