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?

RangeMidpoint 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?

1 / 8

Each range row is followed by its comparison row — watch the range shrink from 9 items to 4, 2, then 1.

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

Watch out: When a range has an even number of items there is no single middle. This lesson always takes the middle-left item. A different choice, used every time, still finds the item — it may just check different midpoints on the way.

Before you trust it

Does the list have to be sorted?

Priya's music app keeps her 500 songs in the order she added them — a total jumble. She wants to use a binary search to find one song.

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

Your turn

Find 31 — you make the calls

Use a binary search to find 31 in the sorted list 4, 9, 13, 18, 22, 27, 31, 36. Whenever the range has an even number of items, take the middle-left item as the midpoint.

  1. Range = all 8 items: 4, 9, 13, 18, 22, 27, 31, 36. Search item: 31.
  2. missing step
Which line is step 2?

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.

7 of 7 still to sort.

Watch out: Being first in the list doesn't make an item quick to find. Binary search starts in the middle, so the middle item is the best case.

Scale it up

1,000 sorted items. How many comparisons, at most?

A sorted list holds 1,000 items. What is the MOST comparisons a binary search could need to find one of them?

Your estimate

500 comparisons

0 comparisons1000 comparisons

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

1Binary search needs sorted data: ascending or descending order.
2Each round: midpoint → compare → keep only the half that could hold the item.
3Even-sized range: this lesson uses the middle-left item as the midpoint.
4Stops when the midpoint matches (found) or the range is empty (not in the list).
5Best case is one comparison: the search item is the first midpoint.

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?
Finds an item in a sorted (ordered) data set by checking the middle item and keeping only the half that could hold it.
What makes a list 'ordered'?
Its values are in ascending order or descending order.
The data is unordered. What are your two options?
Sort the data first and then use a binary search — or use a linear search instead.
In a binary search, what is the 'range'?
The items where the search item could still be. It starts as the whole list.
Ascending list: what happens when the midpoint item is LESS than the search item — and when it's GREATER?
Less: the range becomes the items after the midpoint. Greater: the range becomes the items before it.
When does a binary search stop?
When the midpoint item equals the search item (found), or when there are no items left in the range (not found).
Even number of items in the range: which midpoint does this lesson use?
The middle-left item. It's a convention — a different choice, used consistently, can give a different sequence of comparisons.
What is the best case for a binary search?
The search item is the first midpoint, so it is found with one comparison.
At most how many comparisons to binary search 1,000 sorted items?
10. Doubling to 2,000 items adds only one more: at most 11.
crow, deer, eagle, horse, lion, moose, rhino, tiger, zebra: which animals take the most comparisons to find?
Horse and zebra — four comparisons each (lion, the first midpoint, takes one).
Why is binary search much more efficient than linear search?
Each comparison discards half of the remaining data, so on average it finds an item in less time.

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

See the full OCR Computer Science curriculum →

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.