GCSE · Computer Science · AQA · Spec 8525
Binary search
Guess my number from 1 to 100: higher or lower? You wouldn't start at 1. You'd say 50, then halve what's left, then halve again. That's binary search.
Computer Science · Algorithms
Watch the list shrink
Binary search hunts for 33 in a sorted list of nine numbers. Step through it and keep an eye on 'left to search'.
Before each Next, call it: will the search go left or right of the middle item?
1 low ← 02 high ← LEN(list) − 13 found ← False4 WHILE found = False AND low ≤ high5 mid ← (low + high) DIV 26 IF list[mid] = target THEN7 found ← True8 ELSE IF list[mid] < target THEN9 low ← mid + 110 ELSE11 high ← mid − 112 ENDIF13 ENDWHILE14 IF found = True THEN15 OUTPUT 'Found at position ', mid16 ENDIF
Variables
Output
Step 1: We're looking for 33. The list is sorted, smallest to biggest, and its positions are numbered 0 to 8. low and high mark the two ends of the part we still need to search. Right now that's the whole list.
Use Next one step at a time. The highlighted line is the one running, and the chips show where the search has got to.
Predict, then check
Same nine numbers as before. This time they've been shuffled.
Here's the list, shuffled: 14, 52, 3, 40, 27, 8, 33, 21, 46. Binary search runs on it, looking for 8. The 8 is definitely in there. What happens?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
That's why binary search is fast, and why it only works on a list that's in order.
What you need to know
- Binary search finds an item in a sorted data set.
- It compares the item it wants with the middle item, then discards the half that can't contain it.
- It repeats this on what's left until the item is found.
- It only works on ordered data. With unordered data, use a linear search or sort the data first.
- On average, binary search finds an item in less time than a linear search.
The big picture
Binary search finds an item in a sorted list. It looks at the middle item, compares it with the item it wants, throws away the half that can't contain it, and repeats until the item is found. Because it halves the list every time, it needs remarkably few comparisons, but the halving only makes sense when the list is in order.
Key points
Worked example
Problem
Use binary search to find Jo in this alphabetically sorted list of 11 names (positions 0 to 10): Ali, Ben, Cara, Dev, Ella, Finn, Gus, Hana, Ivy, Jo, Kim.
⚠ Watch out
Keeping the wrong half. In a list sorted smallest to biggest (or A to Z), a target bigger than the middle item (or later in the alphabet) is on the RIGHT, and a smaller one is on the LEFT. And don't leave the checked middle item in the new range.
Memory hook
Middle, compare, bin half, repeat. No order? No binary search.
Check yourself
Cover the page. Binary search looks for 47 in 2, 9, 16, 24, 31, 47, 53. Which item does it check first, what's left after that, and what does it check next?
Flashcards
(9)What must be true of a list before binary search can be used on it?
Which item does binary search check first?
What happens after each comparison that isn't a match?
In a list sorted smallest to biggest, the target is bigger than the middle item. Which half is kept?
Why can binary search only be performed on an ordered list?
Your data is unordered. What are your two options?
On average, which finds an item in less time: binary search or linear search?
How can you work out the middle position of the range from low to high?
When does binary search stop?
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 AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)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 8 November 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.