Spec & Goals 3 min
AQA Spec 3.1.3 · Searching algorithms
By the end of this lesson you can:
- Describe how a linear search checks each item in turn.
- Describe how a binary search halves a sorted list each step.
- Trace a binary search and count the comparisons it makes.
Warm-Up 5 min
Last lesson we measured the efficiency of an algorithm by counting its steps. Today we count comparisons in two ways of searching.
Quick starter
You want to find a friend's name in a phone book. The names are in alphabetical order.
Would you read every name from the start, or open the book in the middle first?
Reveal the idea
Most people open the middle, then jump halfway again. That is a binary search — it only works because the list is sorted. Reading every name from the start is a linear search.
Key Concept — two ways to search 14 min
A searching algorithm finds a value in a list, or confirms the value is not there. AQA expects you to know two of them.
Linear search
A linear search checks each item in turn, starting at the front. It stops when it finds the target or reaches the end of the list.
It works on any list — sorted or not. That is its main strength.
AQA pseudo-code — linear search
found ← False
i ← 0
WHILE i < LEN(list) AND found = False
IF list[i] = target THEN
found ← True
ELSE
i ← i + 1
ENDIF
ENDWHILE
OUTPUT foundThe same in Python
found = False i = 0 while i < len(items) and not found: if items[i] == target: found = True else: i = i + 1 print(found)
Binary search
A binary search is faster, but the list must be sorted first. It works on the middle item each step.
Each step does one of three things:
- If the middle item is the target — stop, it is found.
- If the target is smaller — discard the upper half.
- If the target is larger — discard the lower half.
Then repeat on the half that remains. Each step roughly halves the list.
AQA pseudo-code — binary search
low ← 0
high ← LEN(list) − 1
found ← False
WHILE low ≤ high AND found = False
mid ← (low + high) DIV 2
IF list[mid] = target THEN
found ← True
ELSE IF target < list[mid] THEN
high ← mid − 1
ELSE
low ← mid + 1
ENDIF
ENDWHILE
OUTPUT foundThe same in Python
low = 0 high = len(items) - 1 found = False while low <= high and not found: mid = (low + high) // 2 if items[mid] == target: found = True elif target < items[mid]: high = mid - 1 else: low = mid + 1 print(found)
Worked Example — binary search for 7 12 min
Problem: binary search for 7 in the sorted list [1, 3, 4, 7, 9, 11, 15]. The seven items sit at index 0 to 6.
We set low ← 0 and high ← 6, then halve each step using mid ← (low + high) DIV 2.
| Step | low | high | mid | list[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 7 | 7 = 7 → found |
mid = (0 + 6) DIV 2 = 3, and list[3] is 7. The target is found on the first comparison.
Now trace a target that takes longer — search for 11 in the same list:
| Step | low | high | mid | list[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 7 | 11 > 7 → keep upper half, low ← 4 |
| 2 | 4 | 6 | 5 | 11 | 11 = 11 → found |
Here 11 is found after 2 comparisons. Marks are earned for the correct mid at each step (1), the correct half discarded (1), and the final comparison count (1).
Try It Yourself 12 min
Goal: Trace a linear search for 9 in [4, 2, 9, 7, 1]. How many items do you check before you find it?
Hint: start at index 0 and compare each item in turn. Count each comparison.
Goal: Perform a binary search by hand for 15 in the sorted list [1, 3, 4, 7, 9, 11, 15]. Write down low, high and mid at each step.
Hint: use mid = (low + high) DIV 2 and discard the half that cannot hold 15.
Goal: State what must be true of a list before a binary search can be used on it. Explain why a linear search does not need this.
📝 Exam Practice 10 min
Answer the way the examiner expects — the command word and the marks tell you how much to write.
Describe how a linear search works.
Mark scheme
- Each item is checked in turn, starting from the first / start of the list (1).
- It stops when the target is found, or when the end of the list is reached (1).
State one condition a list must meet for a binary search to be used.
Mark scheme
- The list must be sorted / in order (1).
Give one advantage of a binary search over a linear search on a large list.
Mark scheme
- It makes far fewer comparisons / is faster (because it halves the list each step) (1).
A binary search runs on the sorted list [2, 5, 8, 12, 20] to find 2. State how many comparisons are made.
Mark scheme
2comparisons (1) — checkmid = 8(2 < 8), thenmid = 2(found).
Recap & Key Terms 3 min
A searching algorithm finds a value in a list. A linear search checks each item in turn and works on any list. A binary search halves a sorted list each step, so it is much faster on large lists.
- Searching algorithm
- A method to find a value in a list, or report that it is absent.
- Linear search
- Check each item in turn from the start until the target is found or the list ends. Works on any list.
- Binary search
- On a sorted list, check the middle item and discard the half that cannot hold the target; repeat.
- Sorted list
- A list whose items are in order. Binary search requires one; linear search does not.
Homework 1 min
Task (≤ 15 min): Trace a binary search for 4 in the sorted list [1, 3, 4, 7, 9, 11, 15]. Record low, high and mid at each step, and state the number of comparisons.
Model answer
| Step | low | high | mid | list[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 7 | 4 < 7 → keep lower half, high ← 2 |
| 2 | 0 | 2 | 1 | 3 | 4 > 3 → keep upper half, low ← 2 |
| 3 | 2 | 2 | 2 | 4 | 4 = 4 → found |
The value 4 is found after 3 comparisons. Award marks for: correct mid values (1), correct half discarded each step (1), correct comparison count (1).