Binary Search: The Power of Halving

simulator beginner ~8 min
Loading simulation...
Binary: 7 steps vs Linear: ~50 steps

For 100 elements with the target in the middle, binary search takes about 7 steps (log₂ 100 ≈ 6.6) while linear search takes about 50 — a 7× speedup.

Formula

Binary search: T(n) = O(log₂ n) — at most ⌈log₂(n)⌉ comparisons
Linear search: T(n) = O(n) — up to n comparisons

The Most Elegant Algorithm

Binary search is the simplest and most powerful example of the divide-and-conquer strategy. Given a sorted array and a target value, it checks the middle element and discards half the array with each comparison. This means searching through a million elements takes at most 20 steps. Jon Bentley famously noted that despite its simplicity, most programmers can't write a correct binary search implementation — off-by-one errors are notoriously tricky.

Logarithmic vs Linear

The difference between O(log n) and O(n) is the difference between practical and impractical at scale. A phone book with 1 million names: binary search finds any name in 20 comparisons. Linear search might need all 1 million. A database with 1 billion records: binary search needs 30 steps. Linear search needs up to 1 billion. Every doubling of data adds just one more step to binary search.

Watching the Race

The simulation shows two copies of a sorted array. The linear search (red) checks elements one by one from the left. The binary search (cyan) jumps to the middle, then halves its range. Watch how the binary search's remaining range (shown as a highlighted band) shrinks exponentially while the linear search crawls forward. The step counters make the difference viscerally clear.

Beyond Simple Arrays

The binary search principle extends far beyond arrays. Binary search trees organize data for O(log n) insertion, deletion, and lookup. Git bisect uses binary search to find the commit that introduced a bug. Numerical methods use bisection to find roots of equations. Any time you can halve the problem space with a single test, you're applying the binary search principle — one of the most fundamental ideas in computer science.

FAQ

How does binary search work?

Binary search works on a sorted array. It compares the target to the middle element. If equal, it's found. If the target is less, search the left half. If greater, search the right half. Each step eliminates half the remaining elements, giving O(log n) time complexity.

Why is binary search O(log n)?

Each comparison halves the search space. Starting with n elements: after 1 step there are n/2 left, after 2 steps n/4, and so on. After k steps, n/2^k elements remain. We're done when n/2^k = 1, so k = log₂(n). For 1,000,000 elements, that's just 20 steps.

What are the requirements for binary search?

Binary search requires the data to be sorted and to have random access (ability to jump to any index in O(1) time). It works on arrays but not linked lists. If data isn't sorted, you must sort it first — but for repeated searches on the same data, the O(n log n) sorting cost is amortized.

Where is binary search used?

Binary search is everywhere: database index lookups, dictionary word lookup, finding entries in phone books, git bisect for bug finding, IP routing tables, and as the foundation of binary search trees (BSTs) used in virtually every database and file system.

Sources

Embed

<iframe src="https://homo-deus.com/lab/computer-science/binary-search/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub