← Binary Search · guided lesson

Binary Search

1 / 18 · story

the story

Binary search

Every tool here exists because something was too slow or broke without it. Here is what.

1946 · 1960 · John Mauchly (first mention); D. H. Lehmer (first version for any n)

Binary search was first mentioned by John Mauchly in the 1946 Moore School Lectures, the first course on building electronic computers — as Knuth records in The Art of Computer Programming, Vol. 3. Early published versions only worked when the table had 2ᵏ − 1 entries; D. H. Lehmer published one that works for any n in 1960. It stayed tricky: in Jon Bentley's courses at Bell Labs and IBM, about 90% of professional programmers found bugs in their own binary search (Programming Pearls).

the problem it solved

Finding an item among n sorted ones with about log₂ n comparisons instead of up to n.

why it matters: without it

Every lookup in sorted data would be a full scan; finding a bad commit among 1,000 would take up to 1,000 test runs instead of about 10.

sources: Knuth — The Art of Computer Programming, Vol. 3: Sorting and Searching, §6.2.1 ↗ · Lehmer — Teaching combinatorial tricks to a computer (Proc. Symp. Applied Math. 10, AMS, 1960) ↗ · Bentley — Programming pearls: Writing correct programs (CACM, 1983) ↗

also in this chapter: Lower bound & upper bound (boundary search) · The overflow-safe midpoint · Binary search on the answer