GCSE · Computer Science · Eduqas · Spec C500QS
Binary search
Guess a number from 1 to 1,000 when the only clues are higher or lower: start at 500, then halve again. That is binary search.
Computer Science · Searching
Run the search: call every midpoint
Sorted list, indices 0 to 7: 3, 8, 12, 19, 25, 31, 40, 52. Pick a search item, then call each marked step before it appears. The page runs nothing: every value is worked out in advance.
Range: first = 0, last = the index of the final itemMidpoint: mid = (first + last) DIV 2Compare the item at mid with the search itemEqual: stop, found at midItem at mid is less: first = mid + 1Item at mid is greater: last = mid - 1first is past last (range empty): not found
Variables
Step 1. The range is the whole list: indices 0 to 7. This first step is worked for you.
DIV divides and drops any remainder, so 7 DIV 2 is 3. With an even number of items this lesson takes the middle-left one. Other versions of the search take the middle-right; both work as long as you stick to one.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Find an item in an ordered list by throwing away half of it at every step.
What you need to know
- Binary search finds an item in an ordered list, where the values run in ascending or descending order.
- It only works on ordered data, because the midpoint comparison tells it which half to keep.
Have a goPriya has a jumbled list of scores. She says: "Binary search will still find 41 in there, eventually." Is she right?
No. Binary search needs the list in order first.
Comparing with the midpoint only says which half to keep when the list is ordered. In a jumbled list it could throw away the half holding 41.
- Unordered data needs a linear search, or sorting first and then a binary search.
- Start with a range covering the whole list, find its midpoint item, and compare it with the search item.
- Midpoint item less than the search item: the range becomes the items after it. Greater: the items before it. Equal: stop.
- Each comparison discards half of what is left. Repeat until the item is found, or the data runs out: not found.
- A range is its first and last indices. With an even number of items, this lesson takes the middle-left one.
Have a goSome range runs from index 10 to index 13, four items. Which index holds the middle-left item?
Index 11
The two centre items sit at indices 11 and 12. Middle-left means the left-hand one of that pair, so 11.
- On average binary search is quicker than linear search: 1,000 items need at most 10 comparisons, and 2,000 only one more.
Have a goA binary search of 1,000 items needs at most 10 comparisons. At most how many for 2,000 items?
11 comparisons
Each comparison removes half of the data, so doubling the list adds only one more halving, not another 10.
- Best case: the item is the first midpoint, one comparison. Worst case: it is found last, or is not in the list.
- In code, it is a loop that recalculates the midpoint and moves first or last each time round.
The big picture
Binary search finds an item in an ordered list by comparing with the midpoint and discarding half of the remaining data each time, until the item is found or the data runs out. It needs ordered data and takes very few comparisons.
Key points
Worked example
Problem
A shop keeps nine sorted prices: 2, 5, 9, 14, 20, 27, 33, 41, 50 (indices 0 to 8). Find 27 with a binary search, taking the left of the two centre items whenever the candidates split evenly.
⚠ Watch out
Leaving the midpoint in the range (first = mid or last = mid). It has just been checked, so it must go: first = mid + 1 or last = mid - 1.
Memory hook
Middle, compare, chuck half. Middle, compare, chuck half. Stop when you find it or when nothing is left.
Check yourself
A sorted list has 12 items, indices 0 to 11. Using the middle-left rule, what is the first midpoint index? If that item is greater than the search item, what are first and last next?
Flashcards
(12)What does a binary search do?
What is an ordered list?
Why can binary search only be used on an ordered list?
What can you do if the data is unordered?
What is the range in a binary search?
After comparing, what happens to the range if the midpoint item is less than, or greater than, the search item?
When does a binary search stop?
Which midpoint does this lesson take for an even number of items?
How many comparisons does binary search need at most for 1,000 items? And for 2,000?
What are the best and worst cases for binary search?
How does binary search compare with linear search on average?
What does the binary search loop do on each pass?
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 Eduqas GCSE Computer Science topics
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 18 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.