Skip to content
Menu

Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 15

Searching algorithms: linear and binary search

How linear search and binary search work, writing a linear search in Python, tracing a binary search, and comparing the two algorithms by efficiency and requirements.

Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

Whiteboard video lesson: Searching algorithms: linear and binary search

8-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What your child will learn

  • Describe linear search
  • Write a linear search in Python
  • Describe and trace binary search
  • Compare linear and binary search

The method

  1. Linear: check each item in turn from the start.
  2. Binary: list must be sorted; check the middle.
  3. If target < middle, discard the upper half; else the lower.
  4. Repeat until found or no items left.

Key words: algorithm, linear search, binary search, midpoint, efficiency.

Worked examples

Worked examples from the lesson, with every step.

Question 1

Linear search for 8 in [5, 2, 8, 1]. How many comparisons?

  1. Step 1: 5? no. 2? no. 8? yes.
  2. Step 2: 3 comparisons

Answer: 3

Question 2

Binary search for 23 in [3, 8, 12, 17, 23, 31, 40]. First middle?

  1. Step 1: 7 items → index 3 → 17
  2. Step 2: 23 > 17 → keep right half

Answer: 17

Question 3

Continue: right half is [23, 31, 40]. Next step?

  1. Step 1: 31
  2. Step 2: 23 < 31 → left: [23] → found

Answer: found after 3 comparisons