Quiz Entry - updated: 2026.12.01
Binary search for 7 in the sorted array 0 1 3 4 5 7 8 9 11 14 16 18 19 (indices 0–12): which elements are compared, and in which order?
8, then 3, then 5, then 7: found after four comparisons.
* Each row halves the range between l and h until l = m = h at the key. *
| Step | l | h | m | a[m] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 12 | 6 | 8 | 7 < 8: continue left, h = 5 |
| 2 | 0 | 5 | 2 | 3 | 7 > 3: continue right, l = 3 |
| 3 | 3 | 5 | 4 | 5 | 7 > 5: continue right, l = 5 |
| 4 | 5 | 5 | 5 | 7 | equal: found at index 5 |
Each row cuts the candidate range roughly in half: 13 elements, then 6, then 3, then 1. Thirteen elements need at most $\lfloor \log_2 13 \rfloor + 1 = 4$ comparisons, and that is exactly what this search took.
Notice that binary search never looks at most of the array: 0, 1, 4, 9, 11 and everything above 8 were excluded without ever being compared.