Binary search
About this lesson: Binary search
In short
In a sorted list, check the middle item. If it's too small, discard the left half; if too big, discard the right half. Repeat. Pick a target and step through.

What you'll explore
- Halve it: Check the middle
- In a sorted list, check the middle item. If it's too small, discard the left half; if too big, discard the right half. Repeat.
- How fast?: Linear vs binary
- Linear search might check every item. Binary search halves the list each time. Change the list size and compare the worst cases.
- Why sorted?: Binary search on a jumbled list
- Binary search assumes the list is in order. Try it on an unsorted list where 51 really is present. Step through and see what goes wrong.
- Check yourself: Checks
- A sorted list has 31 items.
Also in this lesson
- Common mix-ups
- Binary search
- Guess my number
Words to know
- binary search
- Searching a sorted list by repeatedly checking the middle.
- linear search
- Checking items one by one.
- sorted
- In order.
- midpoint
- The middle position of the current interval.
- interval
- The part of the list still being searched.
Sources
- BBC Bitesize KS3 Computer Science
- Raspberry Pi Foundation, Computing curriculum resources