This page is a document written with epiphany. One Typst source builds it and its
PDF; every box, storyboard and figure below is a primitive of the
package, and the figure in section 2 runs live.
Binary search, explained
A gallery of epiphany: every primitive of the package, on one running example
Binary search finds a value in a sorted array by asking one question per step and throwing away half of what is left with each answer. This document explains it the way the package is meant to be used: a picture of one example first, then the idea in words, then in symbols.Figures, Tables, Algorithms
How many questions does it take to find one value among a thousand, or a million?
Ten numbers, sorted, and one of them is 23. Checking them from the left finds it on the sixth try, and a value at the end would take ten. The order is wasted: a reader of a dictionary never starts at “a”.
Ask about the middle instead. The probe holds 16, which is less than 23, and because the array is sorted, so is everything to its left. One answer rules out half the array.
Table 1: The notation of this document, in words and in symbols.
In words
In symbols
the sorted array, and its length
the value we look for
the part still worth searching
the probe in its middle
Definition 1 (search range). The search range is the index interval that still may hold ; it is empty when .
Invariant 1 (the target stays in range). If occurs in , then it occurs in .
2 The algorithm
What, exactly, happens on each step?
Algorithm 1: Binary search for in a sorted array .
is too large, so ; then and . Found, on the third probe.
range , probe range , probe range , probe
Figure 1: Every probe of the search for 23: the range shrinks to the one cell that holds it.Figure 2: Pick a target and step through the search; the lines of Algorithm 1 that run light up.
3 Why it works, and how fast
Why can the search never skip over the target, and how many probes can it need?
Proof. A pass that does not return moves past only when , and then every with is at most because is sorted; the case is the mirror image. ∎
Theorem 1 (binary search is correct and fast).Algorithm 1 returns an index of if occurs in , and none otherwise, after at most probes.
Proof. By Lemma 1 the target is never dropped, so the search returns none only once the range is empty. Each pass leaves at most half of the range, so after passes at most indices remain, which is less than one once . ∎
Proposition 1 (the example). For the array of Figure 1, every target takes at most probes; 23 takes 3.
scanning: 6 probesbinary search: 3 probes
Figure 3: Scanning from the left against binary search, for the same target.
4 Variants
What if the target is missing, or appears more than once?
Exact match
Return any index of , or none: Algorithm 1 as written.
Lower bound
The first index with : where would be inserted.
Upper bound
The first index with ; with the lower bound, it counts the copies of .
A Implementation notes
Colophon
This document is written in Typst, the programmable markup language for typesetting that Laurenz Mädje and Martin Haug began at TU Berlin [2], [3]. Typst builds both of its versions from the same source: the PDF, and the web page through its HTML export, where the live figure is a script plugged into the page. Every picture is drawn with Typst’s own shapes, from the data the live figure starts from. The primitives come from epiphany, a Typst package under the MIT No Attribution license.
References
[1] D. E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd ed. Addison-Wesley, 1998.
[2] L. Mädje, “Typst: A Programmable Markup Language for Typesetting,” Master's thesis, 2022.