Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Binary Search: a Quick Intro

Tested 2026-11-28 on Debian GNU/Linux 13 (trixie), Raspberry Pi 4 Model B (4 GB, 4 cores, aarch64); no containers or VMs · with Python 3.13.5 · by the fromzero project · CC BY 4.0

Your shop keeps a sorted list of a million order numbers.

Someone types one in that isn’t there. Reading the list from the top, you can only say so after all 1,000,000 numbers: 30.5 ms on the Raspberry Pi this course was written on (best of 7 runs), for every number that’s missing.

Look at the middle instead, and throw away the half it can’t be in. At most 20 looks: 6.3 µs.

That’s binary search: about ten lines, and easy to get subtly wrong. One - 1 too many and it skips the last number; one + 1 missing and it never stops.

This course has you write it yourself from zero, with one rule that tells you why each boundary is what it is.

By the end you’ll be able to:

  • explain why 20 looks are enough for a million items, and when binary search gives wrong answers;
  • write binary search from scratch with the window [lo, hi), and say why each line keeps it correct;
  • find the first and last copy of a value, and where a missing value would go;
  • spot and fix the off-by-one bugs that skip an element or loop forever.

How it works

Each lesson starts from zero with an animated, step-by-step picture (press Play, or step with the arrow keys), then explains in plain words, with optional Go deeper sections. Each practical has you write the code yourself in ~/binary-search-intro/: requirements, a hint, a hidden solution, a “predict first” question and a part where you break it on purpose. One script, ./lab, does the rest:

  • ./lab setup p0 checks your Python; ./lab setup p1 writes the capstone’s one data file, a day of event times. There’s nothing to download, and every program is yours to write.
  • ./lab check pN tells you whether your work is right. It calls your functions in a separate process with a time limit, so an endless loop is reported instead of freezing your terminal. It only reads.
  • ./lab reset pN starts you at a practical with the earlier ones’ solutions in place. It asks first, and moves your files into a new folder rather than deleting them.

All you need is Python 3.9+ and curl on your own computer. No containers, no virtual machines.

Path

LessonPracticalYou’ll be able to
1 · Why halving beats looking everywhereexplain why 20 looks cover a million items, and why the list must be sorted
2 · The window and its promise0 · Your binary searchwrite binary search with [lo, hi), count its reads, find an endless loop
3 · First, last, and where it would go1 · Capstone: range counts from an empty folderwrite the first/last/insertion-point variants and a tool built on them, tested against bisect
What your folder will look like
 ~/binary-search-intro/
 ├── lab           the lab script: ./lab setup|check|reset pN (Practical 0)
 ├── lab.log       every command ./lab ran, with its output
 ├── bsearch.py    your search(a, x) (Practical 0)
 ├── rounds.py     counts how many elements search reads (Practical 0)
 └── ranges/       the capstone (Practical 1)
     ├── events.txt  100,000 sorted event times (./lab setup p1)
     ├── bounds.py   your lower_bound, upper_bound, first, last, insert_at
     └── count.py    how many events between two times