Binary Search
Sorted data changes everything
A sequential search will work on any list you hand it, in any order, and that flexibility is exactly what makes it slow. With nothing known about the arrangement, the only safe plan is to look at everything.Sort the data first and a plan that is often much faster opens up. The binary search algorithm starts at the middle of a sorted list, throws away the half that cannot possibly contain the target, and repeats on whatever is left.
| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| value | 3 | 13 | 18 | 24 | 29 | 36 | 41 | 47 | 52 |
Each probe does one of three things, and then the whole thing repeats on a range half the size:
1. Read the value at the middle index of the range still in play. If it is the target, stop.
2. If the target is larger than that value, everything from the middle leftwards is too small. Keep the right side.
3. If the target is smaller, everything from the middle rightwards is too large. Keep the left side.
2. If the target is larger than that value, everything from the middle leftwards is too small. Keep the right side.
3. If the target is smaller, everything from the middle rightwards is too large. Keep the left side.
Finding the middle in JavaScript
The range still in play is two index variables, lo and hi, and their halfway point does not always land on an index. A range of lo = 0, hi = 7 puts it at 3.5, and JavaScript has a single number type whose division operator never truncates, so that fraction survives.A midpoint therefore has to be floored by hand before it can be used as an index. Skip that step and
list[3.5] hands back undefined, not an error, just a value that loses every comparison that follows it, so the search wanders off and reports nothing found.Call the floored midpoint
mid. After a probe, the range shrinks past the middle. If the probe read a value below the target, lo becomes mid + 1; if it read a value above the target, hi becomes mid - 1. Once lo passes hi, the range is empty and the search reports that the value is absent.Counting the work
Every probe reads one value, and unless that value is the target it buys the right to throw away half of what remains. Start with 200 candidates: 200, 100, 50, 25, 12, 6, 3, 1. Seven halvings bring the field down to a single survivor.That survivor has not been read yet, and reading it is what settles whether the target is present at all. So the number of values examined is one more than the number of halvings: eight probes for two hundred candidates.
For a list of n values, a sequential search examines about n in the worst case. A binary search examines about log base 2 of n, which is just the number of times n can be halved.
| list size | sequential, worst case | binary, worst case |
|---|---|---|
| 31 | 31 | 5 |
| 200 | 200 | 8 |
| 5,000 | 5,000 | 13 |
| 100,000 | 100,000 | 17 |
The program below halves the number of candidates, rounding down, until one is left, and then adds one probe for that last candidate. That total is the most values a binary search of
candidates sorted values examines. Run it, then change candidates and run it again:• Set
candidates to 31, the first row of the table.• Set
candidates to 1000, twice as many.• Set
candidates to 1.let candidates = 500;
let halvings = 0;
while (candidates > 1) {
candidates = Math.floor(candidates / 2);
halvings += 1;
console.log("Halving", halvings, "leaves", candidates);
}
console.log("Most values examined:", halvings + 1);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.