Linear and binary search in code
Knowing how a search works is one thing; writing and tracing one is what the exam asks. Here are both searches as real Python programs.
Linear search in Python
names = ["Ola", "Ben", "Mia", "Raj", "Zoe"]
target = "Raj"
found = False
index = 0
while index < len(names) and not found:
if names[index] == target:
found = True
else:
index = index + 1
if found:
print("Found at", index)
else:
print("Not found")
Output: Found at 3
Linear search works on unsorted lists. In the worst case it checks every item.
Binary search in Python
items = [3, 8, 12, 15, 23, 31, 40, 52, 67]
target = 52
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 items[mid] < target:
low = mid + 1
else:
high = mid - 1
if found:
print("Found at", mid)
else:
print("Not found")
Output: Found at 7
Tracing a binary search
| low | high | mid | items[mid] | Action |
|---|---|---|---|---|
| 0 | 8 | 4 | 23 | 52 is bigger: low = mid + 1 |
| 5 | 8 | 6 | 40 | 52 is bigger: low = mid + 1 |
| 7 | 8 | 7 | 52 | Found |
How efficient are they?
Linear search on 1000 items may need 1000 comparisons. But binary search needs the data sorted first, so for a small or unsorted list a linear search can be the better choice.
Use the binary search program to search for 15. Give the values of mid in order.
- low = 0, high = 8, so mid = 4; items[4] = 23, 15 is smaller, so high = 3.
- low = 0, high = 3, so mid = 1; items[1] = 8, 15 is bigger, so low = 2.
- low = 2, high = 3, so mid = 2; items[2] = 12, 15 is bigger, so low = 3.
- low = 3, high = 3, so mid = 3; items[3] = 15, found.
Answer: 4, 1, 2, 3
Linear search checks every item in turn and works on any list. Binary search needs a sorted list and halves the search area each time, using low, high and mid = (low + high) // 2.
- Binary
- Base 2: the number system that uses only the digits 0 and 1.
- Binary search
- Repeatedly checks the middle item of a sorted list and discards half the list.
- Linear search
- Checks each item in turn from the start of a list until the target is found or the list ends.
- Trace table
- A table that tracks the value of each variable as an algorithm runs.
All GCSE and International GCSE Computer Science key terms and definitions โ
Try two quick questions
See if it has sunk in. Answers are marked straight away.
2 more questions
Try each one, then tap to check.
What does this linear search program print?
names = ["Ola", "Ben", "Mia", "Raj", "Zoe"]
target = "Zoe"
found = False
index = 0
while index < len(names) and not found:
if names[index] == target:
found = True
else:
index = index + 1
if found:
print("Found at", index)
else:
print("Not found")Show the answer
Answer: Found at 4
Zoe is at index 4 (counting from 0), so it prints Found at 4.
A linear search looks for an item stored at index 8 of a list. How many comparisons does it make to find it?
Show the answer
Answer: 9
It checks indexes 0 to 8: that is 9 comparisons.
Check you have got it
Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.
- Pearson Edexcel GCSE Computer Science (1CP2) specification, topic 1.2.6, 6.2.2
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 1.1.7, 1.1.8
Checked by the GCSE-ready editorial team, 17 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.
What do exam command words like explain and evaluate mean? โ