GCSE · Computer Science · OCR · Spec J277
Binary search
Hunting for 'otter' in a dictionary? Open it in the middle, land on 'lemon', and the whole first half is out. Do that again and again — that's binary search.
Computer Science · Searching
Hunt for the horse
Sorted list, A to Z: crow, deer, eagle, horse, lion, moose, rhino, tiger, zebra. Search item: horse.
Go one step at a time. Before each step, predict: which item is the midpoint, and which half survives?
| Range | Midpoint vs horse |
|---|---|
| crow … zebra |
Output
Step 1: Start with the whole list as the range: all 9 animals, every place horse could be. Nine is odd, so there is one true middle item. Which is it?
Each range row is followed by its comparison row — watch the range shrink from 9 items to 4, 2, then 1.
Best case, worst case
How many comparisons to find each animal?
Same sorted list: crow, deer, eagle, horse, lion, moose, rhino, tiger, zebra. Taking middle-left midpoints, how many comparisons does a binary search need to find each animal?
Still to sort
1 comparison (0)
Found at the very first midpoint.
2 comparisons (0)
3 comparisons (0)
4 comparisons (0)
The most any animal in this list needs.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Check the middle, bin half, repeat — a seriously quick way to find something in a sorted list.
What you need to know
- Binary search is an algorithm for finding an item in a sorted data set.
- Sorted (ordered) means the values run in ascending order, like A to Z, or descending order, like largest to smallest.
Have a goQuick sort-out: is the list 90, 72, 55, 31, 8 ordered, so a binary search could be used on it?
Yes — it's in descending order.
Ordered doesn't have to mean smallest first. Largest to smallest is still in order, so it counts.
- Unsorted data? Binary search won't work on it — sort the data first, or use a linear search instead.
- Your range starts as the whole list: every item where the search item could still be hiding.
- Each round, find the midpoint item of the range and compare it with the search item.
- In an ascending list: midpoint smaller than the search item? Keep the items after it. Bigger? Keep the items before it.
Have a goSam is searching 2, 5, 9, 14, 20, 26, 33 for 20. The midpoint is 14, and Sam announces, very confidently, 'Keep the left half!' Is Sam right?
No — keep the items after 14: 20, 26, 33.
14 is smaller than 20, so in an ascending list 20 can only be further along. Everything to the left of 14 is even smaller.
- Midpoint equal to the search item? Stop — you've found it.
- An even number of items has no single middle, so this lesson takes the middle-left item. Other consistent choices are possible.
Have a goThe range is bat, cat, dog, eel. Using middle-left, which item is the midpoint?
cat
Four items have no single middle. The two middle items are cat and dog, and middle-left takes cat — not an average, an actual item.
- Every comparison discards half of what's left. Repeat until the item turns up or the range is empty — then it isn't there.
- That halving makes it quick: on average faster than a linear search, and 1,000 items need at most 10 comparisons.
The big picture
Binary search finds an item in a sorted list by checking the middle item of the range, keeping only the half that could still hold the search item, and repeating until it finds the item or runs out of items.
Key points
Worked example
Problem
Use a binary search to look for 14 in the sorted list 3, 8, 12, 15, 21, 26, 30. Show each comparison and say whether 14 is found.
⚠ Watch out
Keeping the wrong half. In an ascending list, a midpoint that is smaller than the search item means the item is further along — keep the items after the midpoint, not before it. And the midpoint itself is never kept: it has already been checked.
Memory hook
Mid. Compare. Chop. Repeat. — find the middle, compare, chop off the half it can't be in, and go again until you find it or run out.
Check yourself
Cover the page. Can you say why binary search needs sorted data, what happens after each of the three possible comparison results, and how to pick the midpoint of an even-sized range?
Flashcards
(11)What does a binary search do?
What makes a list 'ordered'?
The data is unordered. What are your two options?
In a binary search, what is the 'range'?
Ascending list: what happens when the midpoint item is LESS than the search item — and when it's GREATER?
When does a binary search stop?
Even number of items in the range: which midpoint does this lesson use?
What is the best case for a binary search?
At most how many comparisons to binary search 1,000 sorted items?
crow, deer, eagle, horse, lion, moose, rhino, tiger, zebra: which animals take the most comparisons to find?
Why is binary search much more efficient than linear search?
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 OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 9 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.