Mission control wants Beacon to answer search questions quickly, even when the log gets long.
Comet deals thirty-two paper cards face down in a long row, numbered 1 to 32 in order.
"Pick a secret number," she tells Wren. "I bet I can find it in fewer than ten guesses."
Wren writes a number on a sticky note and hides it. "Go ahead. I will only say higher, lower or found."
Nova hovers above the row. "Interesting," she says. "Count how many cards are still in play after each guess."
Comet flips over card 16. "Guess one."
You will play guess-the-number with sorted cards, using binary search.
You will record how many cards are left after each guess, then find the most guesses this game can ever need.
| Guess | Cards in play | You guess | Partner says | Cards left after |
|---|---|---|---|---|
| 1 | 1 to 32 | 16 | Higher | 17 to 32 (16 cards) |
| 2 | 17 to 32 | 24 | Lower | 17 to 23 (7 cards) |
| 3 | 17 to 23 | 20 | Higher | 21 to 23 (3 cards) |
| 4 | 21 to 23 | 22 | Higher | 23 only (1 card) |
| 5 | 23 only | 23 | Found | Done |
Count the halvings in the worst case: 32, then 16, 8, 4, 2 and 1 card in play.
That is five halvings. One more guess on the last card makes six guesses at most.
| What the lab shows | True or false? |
|---|---|
| Each binary search guess eliminates about half of the cards in play. | ? |
| Binary search would still work if the cards were shuffled. | ? |
| Binary search never needs more than 6 guesses for 32 sorted cards, using this rule. | ? |
| Linear search never needs more than 6 guesses for 32 cards. | ? |
Great lab work. Tomorrow you will meet problems that even fast computers find hard.