OCR J277 · 2.1.3 Searching and Sorting Algorithms

Binary Search

Mr Brown

What Is a Binary Search?

A binary search looks for an item in a sorted list. It checks the middle item, then discards the half of the list that cannot contain the item being searched for (the target). It repeats this until the target is found or there are no items left.

Interactive Demo

Watch a Binary Search

Items checked 0

Key points

  • Check the middle item. If it is not the target, discard half of the list and repeat with the items that are left
  • The list must be sorted
  • Best case: the target is the middle item. It is much faster than a linear search on a large list

Try It

Interactive Practice

What Happens Next?

The middle item has been checked. Decide what the search does next.

Items checked 1
Right first time 0 / 0
Interactive Practice

Complete the Search

Work out the whole search, then fill in the boxes.

Searches right 0 / 0

The Algorithm

In the exam, a binary search may be shown as code or in OCR Exam Reference Language. You don't need to remember it, but you must recognise it and explain what each part does. Click a line to see what it does, or step through it.

Interactive Demo

Run the Code

target
low
high
mid
found
list
Output: –

How to spot a binary search

  • Variables called low, high and mid (or similar)
  • A middle index worked out with (low + high) DIV 2
  • An if / elseif / else that compares list[mid] with the target and moves low or high
  • A loop that runs while low <= high, so it stops when no items are left

Sample Questions

Write your answer, then reveal the mark scheme and tick each point you included.

Interactive Practice

Write & Mark

Interactive Practice

Complete the Explanation

Choose the right word for each gap.

Binary Search Challenges

Complete

0

Points

0
Challenge

Loading challenge…