GCSE · Computer Science · Edexcel · Spec 1CP2
Binary search
Find one item among 1,000 in at most ten comparisons. That's the trick binary search pulls off.
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 is 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 only works on ordered data: items in ascending or descending order.
- Each round: find the midpoint item of the range, compare it with the search item, then keep only the half that could still hold it.
- If the range has an even number of items, the midpoint is the middle-left item; if only one item is left, that item is the midpoint.
- The search stops when the midpoint matches the search item, or reports 'not found' when the range runs out.
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. Once it has 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 30 September 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.