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 ← 0
2 high ← LEN(list) − 1
3 found ← False
4 WHILE found = False AND low ≤ high
5 mid ← (low + high) DIV 2
6 IF list[mid] = target THEN
7 found ← True
8 ELSE IF list[mid] < target THEN
9 low ← mid + 1
10 ELSE
11 high ← mid − 1
12 ENDIF
13 ENDWHILE
14 IF found = True THEN
15 OUTPUT 'Found at position ', mid
16 ENDIF

Variables

low=0mid=—high=8target=33list[mid]=—comparisons=0left to search=3 8 14 21 27 33 40 46 52

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.

1 / 9

Use Next one step at a time. The highlighted line is the one running, and the chips show where the search has got to.

Step 1 of 9: 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..

Exam line: Describe all three moves, every time: find the middle item, compare it with the item you want, discard the half that can't contain it. Then say what happens next.
Watch out: Once the middle item has been checked, it's ruled out too. The new range starts one after it (mid + 1) or ends one before it (mid − 1).

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?

Computer Science · Algorithms

How many comparisons for 1,000 items?

A sorted list holds 1,000 items. What's the MOST comparisons binary search could need to find an item in it?

Your estimate

500 comparisons

0 comparisons1000 comparisons

Computer Science · Algorithms

Spot the slip

A student used binary search to look for 23 in this sorted list (positions 0 to 10): 5, 12, 17, 23, 30, 38, 44, 51, 59, 66, 72. Their answer says 23 isn't there. Which line goes wrong?

A student's trace — which line goes wrong?

Computer Science · Algorithms

Explain it in your own words

Explain how a binary search would find 45 in this sorted list: 4, 11, 19, 26, 38, 45, 57. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1The three moves, repeated: find the middle item, compare it with the target, discard the half that can't hold the target.
2In a list sorted smallest to biggest, a target bigger than the middle item is on the right; a smaller one is on the left.
3The middle item is ruled out once it's been checked, so the new range starts at mid + 1 or ends at mid − 1.
4If there's nothing left to search, the item isn't in the list.
5Halving is powerful: a sorted list of 1,000 items needs at most 10 comparisons.

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?
It must be sorted (in order).
Which item does binary search check first?
The middle item of the part still being searched.
What happens after each comparison that isn't a match?
The half that can't contain the item is discarded, and the search repeats on what's left.
In a list sorted smallest to biggest, the target is bigger than the middle item. Which half is kept?
The right half, where the bigger items are.
Why can binary search only be performed on an ordered list?
It works out where the item should be from a mid-point. That only means something when the data is in order.
Your data is unordered. What are your two options?
Use a linear search, or sort the data first and then use binary search.
On average, which finds an item in less time: binary search or linear search?
Binary search.
How can you work out the middle position of the range from low to high?
One common way is (low + high) DIV 2: add the two end positions, halve, and ignore any remainder.
When does binary search stop?
When the item is found, or when there's nothing left to search, which means the item isn't there.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.