Theory

Watch: lo, hi, mid, probe by probe

There is no code here yet. What is here is what the search does — probe by probe, on one eleven-item library and four titles to look for.

You write nothing in this step. Read the tables, and in the next step you will recognise the code at once.

index      0    1    2    3    4    5    6    7    8    9    10
           Ada  Bob  Cai  Dan  Eva  Fay  Gus  Hal  Ivy  Jon  Kim

How we count

  • probe — one comparison items[mid].Title < target; the same thing the code will count with c.Hit();
  • candidates — how many slots still fit in [lo, hi), that is hi − lo. The invariant from step 1 says the answer is somewhere among them (or exactly at hi).

Looking for Gus

probe  lo  hi  mid  items[mid]   comparison      does       candidates after
1      0   11  5    Fay          Fay <  Gus      lo = 6     5
2      6   11  8    Ivy          Ivy >= Gus      hi = 8     2
3      6   8   7    Hal          Hal >= Gus      hi = 7     1
4      6   7   6    Gus          Gus >= Gus      hi = 6     0
                                 lo = hi = 6  →  returns 6

Total cost: 4 probes. A linear scan for the same target looks at indices 0–6, that is 7 comparisons.

Looking for Fez — it is not in the library

probe  lo  hi  mid  items[mid]   comparison      does       candidates after
1      0   11  5    Fay          Fay <  Fez      lo = 6     5
2      6   11  8    Ivy          Ivy >= Fez      hi = 8     2
3      6   8   7    Hal          Hal >= Fez      hi = 7     1
4      6   7   6    Gus          Gus >= Fez      hi = 6     0
                                 lo = hi = 6  →  returns 6

The same path, the same answer: 6. Fez is not in the library — 6 is where it would sit.

Looking for Zed — past the last one

probe  lo  hi  mid  items[mid]   comparison      does       candidates after
1      0   11  5    Fay          Fay <  Zed      lo = 6     5
2      6   11  8    Ivy          Ivy <  Zed      lo = 9     2
3      9   11  10   Kim          Kim <  Zed      lo = 11    0
                                 lo = hi = 11  →  returns 11

3 probes against 11 for the linear scan. The answer 11 is one past the last index (len = 11).

Looking for Aaa — before the first

probe  lo  hi  mid  items[mid]   comparison      does       candidates after
1      0   11  5    Fay          Fay >= Aaa      hi = 5     5
2      0   5   2    Cai          Cai >= Aaa      hi = 2     2
3      0   2   1    Bob          Bob >= Aaa      hi = 1     1
4      0   1   0    Ada          Ada >= Aaa      hi = 0     0
                                 lo = hi = 0  →  returns 0

4 probes. The linear scan stops here after 1 comparison.

Side by side

looking for returns binary linear
Gus 6 4 7
Fez 6 4 7
Zed 11 3 11
Aaa 0 4 1

Three observations

1. Every probe throws away about half. The candidate count goes 11 → 5 → 2 → 1 → 0. Eleven items never need more than 4 probes, because 11 can only be halved about 3.5 times. For a hundred thousand it is 17.

2. Gus is found at probe 4, yet the loop does not stop there. Probe 4 sees Gus >= Gus and does hi = mid, not discarding mid — it might be the answer, and it is. The loop ends only when lo and hi meet. Also, Zed shows why hi starts at len and not len − 1: "not found, it would go here" is a legal answer, and Zed returns it — 11.

3. Binary does not win every time. Aaa costs 4 probes where the linear scan needs 1. That is not a bug; it is a first hint of what is coming: "log n is less than n" is true in counts, but not for every target and not on every machine.

What next

In the next step you write the same loop as Go code — alongside the linear scan and sort.Search — and take a counter to check these tables probe by probe.