Codele / Algorithms
Binary search
Guess a number from 1 to 100 and a friend says “higher” or “lower”. You never guess from the start: you guess the middle and throw half away. That is binary search.
- Binary search needs a sorted row. It looks at the middle card first.
- If the middle is too small, everything to its left is too small as well, so that half is thrown away. Too big: throw away the right half.
- Each look cuts the row in half, so 1000 cards need about 10 looks: O(log n).
- Linear search needs up to 1000 looks. Binary is far faster on long rows, but only when the row is sorted.
- On a very short row, or with a lucky target, linear search can win.
Loading the playable lesson…