GCSE · Computer Science · Edexcel · Spec 1CP2
Binary search
One item hiding among 1,000. You get ten comparisons, tops. Sounds impossible? Binary search does it every time.
Computer Science · Searching
Watch the list shrink
You already know this trick. Guess a number from 1 to 15 by always going for the middle, and you'll never need more than four guesses. Binary search uses the same idea on a list. Step through the search for flute and watch the range get smaller.
Before each step, predict: which item is the midpoint, and which half survives?
1 take the ordered list and the search item2 range = the whole list3 midpoint = the item in the middle of the range4 compare the midpoint with the search item5 if midpoint < search item: range = the items after the midpoint6 if midpoint > search item: range = the items before the midpoint7 if midpoint = search item: stop, the item is found8 repeat from line 3 on the new range9 if the range runs out: report 'not found'
Variables
Output
Step 1: The list is in alphabetical order, so it's ordered. That's the whole reason the next trick works. We're hunting for flute.
Use Next and Back to move through the search. The range is where flute could still be; everything in 'ruled out' is gone for good.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Check the middle, throw away half, repeat.
What you need to know
- Binary search finds an item in a sorted data set, where values sit in ascending or descending order.
Have a goYour classmate looks at violin, piano, oboe, harp and groans: 'It's backwards, so binary search is off.' Are they right?
No. It's in descending order, and that still counts as ordered.
Ordered means ascending OR descending. Reverse alphabetical is still an order, so the list is fine to binary search.
- No order, no binary search. Use a linear search instead, or sort the data first and then binary search it.
- You track a range: where the item could still be. It starts as the whole list, marked by its first and last indices.
- Each round, pick the midpoint item of the range and compare it with the search item.
- Midpoint less than the search item? Keep the items after it. Greater? Keep the items before it.
- Even number of items in the range? Take the middle-left one. Just one item left? That's the midpoint.
Have a goThe range is 12, 17, 21, 26, 30, 35. Which number do you compare with the search item?
21
Six items is even, so there's no single middle: take the middle-left item, the 3rd. 26 is the middle-right one, the tempting wrong pick.
- It stops on a match. If the range runs out with no match, it reports 'not found'.
Have a goYou binary search 2, 5, 8 for 6. It compares 5, then 8. What does the search report?
Not found.
6 is less than 8, so keep the items before 8, but none are left in the range. It never reports the nearest value, like 5 or 8, only an exact match.
- Here's the speed secret: every comparison throws away half of what's left.
- Best case is one comparison, when the item is the first midpoint. A 9-item list needs four at most.
- Searching 1,000 items takes at most 10 comparisons; double to 2,000 and it's at most one more.
The big picture
Binary search finds an item in an ordered list by checking the item in the middle and throwing away the half that can't contain what you're looking for. It repeats on the half that's left until it finds a match, or until nothing is left, which means the item isn't there. It only works on ordered data; for unordered data, use a linear search or sort the data first. Because every comparison halves the data, it is very fast: 1,000 items need at most 10 comparisons.
Key points
Worked example
Problem
Use a binary search to find 30 in the ordered list 3, 8, 12, 17, 21, 26, 30, 35, 41, 47. Show each midpoint and which part of the list is kept.
⚠ Watch out
Putting the midpoint back into the new range. It's tempting, because it was 'in the middle'. But once it's been compared and isn't a match, it's out: the new range is only the items before it or after it.
Memory hook
Middle. Compare. Bin half. Repeat. It's like hunting for a word in a dictionary: open it in the middle, decide which side your word is on, and never look at the other half again.
Check yourself
A list of 9 items needs at most 4 comparisons. Without tracing anything, what is the most a list of 18 items could need, and why?
Flashcards
(14)What does a binary search do?
What is an ordered list?
Why does binary search only work on ordered data?
The data isn't in order. What are your two options?
In a binary search, what is the 'range'?
The midpoint item is LESS than the search item. What is the new range?
The midpoint item is GREATER than the search item. What is the new range?
How do you pick the midpoint when a range holds an even count, such as 4 or 6?
Only one item is left in the range. Which is the midpoint?
What are the two ways a binary search can end?
How much of the remaining data does each comparison discard?
What is the best case for a binary search?
Most comparisons needed for a list of 1,000 items?
The list doubles from 1,000 to 2,000 items. What happens to the most comparisons needed?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.