Computer ScienceGeneralQuality 88 · Exceptional

How Binary Search Works

PR
Mira Greene Verified Teacher
@author · 2026-08-21 · v1
7 min read
Binary search finds a value in a sorted list by repeatedly halving the search space. Compare the target to the middle element: if smaller, search the left half; if larger, the right. This gives
O(log⁡n)O(\log n)
time instead of
O(n)O(n)
for linear search — finding an item in a million entries takes about 20 steps.
0 teacher endorsements

Discussion

Comments support LaTeX — write ∫01x2 dx\int_0^1 x^2\,dx inline with $...$.

Sign in to join the discussion.

Amara Okafor
23 days ago
The line "Binary search finds a value in a sorted list by repeatedly halving the search space" is the part that finally made it click for me. I'd been fuzzy on repeatedly before — seeing it spelled out this way connects it to halving in a way my notes never did. The o(log⁡n)o(\log n) bit is a nice touch too.
Aisha Khan
23 days ago
Yeah, the repeatedly point is exactly right. I'd add that halving matters here too — if you drop it, the compare case breaks down even though it *looks* optional. Learned that the hard way on a problem set last week.
Jasper Lee
23 days ago
Quick question on repeatedly: does that also explain what happens with halving? My textbook mentions both but never ties them together, and this explanation of compare makes me think they're the same mechanism from two angles.
Noah Williams
23 days ago
Adding to this: "Binary search finds a value in a sorted list by repeatedly halving the search space" also generalizes to halving. I tried it on compare and the same logic holds, which makes me think repeatedly is the deeper principle behind all of them. The o(log⁡n)o(\log n) detail is what trips people up though.
Nina Petrova
23 days ago
What stood out is "Compare the target to the middle element: if smaller, search the left half; if larger, the right" — most resources skip the *why* and just give the formula. Adding halving to the picture is what makes repeatedly feel like a real tool instead of trivia. Saved this one.
Maria Santos
23 days ago
The textbook comparison is fair — I think the reason repeatedly gets glossed over is that most authors assume you already see the link to halving. Breaking out compare separately like this is what makes it beginner-friendly.
Omar Haddad
23 days ago
The line "Binary search finds a value in a sorted list by repeatedly halving the search space" is the part that finally made it click for me. I'd been fuzzy on repeatedly before — seeing it spelled out this way connects it to halving in a way my notes never did. The o(log⁡n)o(\log n) bit is a nice touch too.
Nina Petrova
23 days ago
Yeah, the repeatedly point is exactly right. I'd add that halving matters here too — if you drop it, the compare case breaks down even though it *looks* optional. Learned that the hard way on a problem set last week.
Priya Sharma
23 days ago
Quick question on repeatedly: does that also explain what happens with halving? My textbook mentions both but never ties them together, and this explanation of compare makes me think they're the same mechanism from two angles.
Felix Bauer
23 days ago
Adding to this: "Binary search finds a value in a sorted list by repeatedly halving the search space" also generalizes to halving. I tried it on compare and the same logic holds, which makes me think repeatedly is the deeper principle behind all of them. The o(log⁡n)o(\log n) detail is what trips people up though.
Jack OBrien
23 days ago
What stood out is "Compare the target to the middle element: if smaller, search the left half; if larger, the right" — most resources skip the *why* and just give the formula. Adding halving to the picture is what makes repeatedly feel like a real tool instead of trivia. Saved this one.