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 item
Midpoint: mid = (first + last) DIV 2
Compare the item at mid with the search item
Equal: stop, found at mid
Item at mid is less: first = mid + 1
Item at mid is greater: last = mid - 1
first is past last (range empty): not found

Variables

last=7first=0target=19

Step 1. The range is the whole list: indices 0 to 7. This first step is worked for you.

Step 1 of 3Called right: 0 of 2

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.

Computer Science · Searching

Which list can you run it on?

You have a list of 500 test scores and you want to know whether a score of 41 is in it. Someone says: "Use binary search, it is the fast one."

Which is closest to what you think right now?
How sure are you?

Exam line: Binary search needs an ordered list, ascending or descending. For unordered data, use a linear search or sort first.

Computer Science · Searching

How many comparisons for 1,000 items?

A sorted list holds 1,000 items. At most how many comparisons will a binary search need? Slide to your guess.

Your estimate

500 comparisons

0 comparisons1000 comparisons

Exam line: Each comparison eliminates half of the data, so 1,000 items need at most 10 comparisons.

Binary search or linear search?

Binary searchvsLinear search

Read across each row and notice which search needs more from the data.

Focus

What the data must be like

Binary search

Ordered (ascending or descending)

Linear search

Anything: ordered or unordered

The insight

This is the first thing to check. Unordered data means a linear search, or sorting first.

Items ruled out per comparison

Binary search

Half of what is left

Linear search

One item

Typical speed

Binary search

On average, finds the item in less time

Linear search

On average, takes longer

Best case

Binary search

The item is the first midpoint: one comparison

Linear search

The item is the first one checked: one comparison

Worst case

Binary search

The item is found last, or is not in the list

Linear search

The item is the last one checked, or is not in the list

Computer Science · Searching

Write the loop

Build a binary search in pseudo-code (indices start at 0). Pick the right line at each gap.

  1. first = 0The range starts as the whole list.
  2. last = LEN(list) - 1LEN(list) is the number of items, so the last index is one less.
  3. found = False
  4. WHILE first <= last AND found == FalseKeep going while the range still has items and the item has not turned up.
  5. missing step
Which line is step 5?

Exam line: The loop recalculates mid every pass, then moves first up to mid + 1 or last down to mid - 1, and reports not found if the item never turns up.

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

1Binary search needs an ordered list, ascending or descending. Otherwise use a linear search, or sort first.
2Find the midpoint item of the range and compare it with the search item. Less: keep the items after it. Greater: keep the items before it. Equal: stop.
3The range is given by first and last indices; the checked midpoint leaves the range, so first becomes mid + 1 or last becomes mid - 1.
4Each comparison discards half of the remaining data, so 1,000 items need at most 10 comparisons and doubling adds only one.
5Stop when the item is found, or when the range is empty and the item is reported as not found.

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?
It finds an item in a sorted (ordered) data set by comparing with a midpoint and discarding half of the remaining data each time.
What is an ordered list?
A list whose values are in either ascending or descending order.
Why can binary search only be used on an ordered list?
It finds where the search item should be relative to a selected midpoint, which only works if the data is in order.
What can you do if the data is unordered?
Use a linear search, or sort the data first and then do a binary search.
What is the range in a binary search?
The part of the list where the search item might still be. It starts as the whole list and is given by the indices of its first and last items.
After comparing, what happens to the range if the midpoint item is less than, or greater than, the search item?
Less: the range becomes the items after the midpoint. Greater: the range becomes the items before the midpoint.
When does a binary search stop?
When the midpoint item equals the search item (found), or when the data is exhausted and the item is reported as not found.
Which midpoint does this lesson take for an even number of items?
The middle-left item. Middle-right is another valid convention; choose one and stay consistent.
How many comparisons does binary search need at most for 1,000 items? And for 2,000?
At most 10 for 1,000 items. Doubling to 2,000 increases the maximum by only one.
What are the best and worst cases for binary search?
Best: the item is the first midpoint (one comparison). Worst: the item is found last, or is not in the list.
How does binary search compare with linear search on average?
On average, binary search can locate an item in less time than a linear search.
What does the binary search loop do on each pass?
It recalculates the midpoint, then updates first or last, until the item is found or the range is empty.

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 Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

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.