Finding one value inside a million is the most common job in computing — the trick is to stop looking at values you can already rule out.
1 Two contestants
Linear search checks every element one by one. Binary search needs sorted data — but pays off massively.
2 The sorted shortcut
If a[mid] is smaller than the target, everything left of mid is smaller too. Half the list dies in one look.
3 Halving power
1,024 elements take at most 10 comparisons; 1,000,000 take about 20. Each comparison halves the window.
4 Where you meet it
Dictionary pages, git bisect, database indexes and autocomplete all ride on binary search or its cousins.
5 Python extras
x in lst is a linear scan. The bisect module gives you a tuned, C-implemented binary search for sorted lists.
Why search fast
Two contestants1Linear search checks every element one by one. Binary search needs sorted data — but pays off massively. The sorted shortcut2If a[mid] is smaller than the target, everything left of mid is smaller too. Half the list dies in one look. Halving power31,024 elements take at most 10 comparisons; 1,000,000 take about 20. Each comparison halves the window. Where you meet it4Dictionary pages, git bisect, database indexes and autocomplete all ride on binary search or its cousins. Python extras5x in lst is a linear scan. The bisect module gives you a tuned, C-implemented binary search for sorted lists. 5 building blocks make up Why search fast
Ten sorted scores, one target. Watch low, mid and high squeeze it to a single cell.
1 / 9A sorted list of 10 scores — we are hunting for 60. The window starts as the whole list: low = 0, high = 9.
One loop, one compare, one halve — the whole algorithm fits in a handful of boxes.
process decision input / output start / end The same hunt in real Python — binary search first, then the linear scan for comparison.
moduledef stores the body — nothing runs until the call.
Memory
binary_search= <function>
Console
output appears as the program runs…
Same target, no sorted order — linear search must peek into every box until it stumbles on 60.
1 / 5An unsorted list — binary search is off the table. Linear search starts at index 0 and walks right.
Trace low, mid and high by hand — // floors, and every probe counts.
Your predicted output — one line per console line
Hint: Probe 1: (0 + 6) // 2 = 3, a[3] is 11 — under 17, so low jumps to 4. Probe 2: (4 + 6) // 2 = 5 — a[5] is 17, bingo.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?sorted inputdrops into place
?(low + high) // 2drops into place
?O(log n)drops into place
?O(n)drops into place
?low > highdrops into place
?bisectdrops into place
?return middrops into place
?unsorted listdrops into place
01Binary search trades one requirement — sorted order — for O(log n) speed.
02Every comparison halves the window: 10 items in 4 probes, a million in about 20.
03// floors the division: (0 + 9) // 2 is 4, never 4.5.
04low > high means the window is empty — return -1.
05Linear search works anywhere but pays O(n); fine for small or unsorted data.
06bisect.bisect_left finds insertion points in sorted lists — binary search without writing the loop.