Revision Notes
OxfordAQA A-Level Computer Science (9645): Searching algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.4.1: linear and binary search, code, traces, comparison of efficiency and conditions, and a self-test.
- Subject
- Computer Science
- Level
- AS LEVEL
- Topic
- Topic 4 – Searching and sorting algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (AS Level)
- 3.4.1 Searching algorithms
This page covers 1 of the 2 subtopics in topic 4, Searching and sorting algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.4.1 Searching algorithms of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (AS content in topic 3.4 Searching and sorting algorithms). For full traces, see the searching algorithms study guide; then practise with the searching algorithms practice questions.
What the specification requires
- Know the linear search algorithm and trace it. You may be asked to write code for it.
- Know that an inefficient linear search always looks at every item, while a more advanced version stops as soon as the item is found.
- Know the binary search algorithm, understand how it operates, trace it, and possibly write code for it.
- Compare them on time efficiency and on the conditions for use (does the list have to be ordered?).
- Formal Big O comparisons are not required at AS but may be required at A-level.
Key terms
| Term | Meaning |
|---|---|
| Target | The value being searched for |
| Linear search | Checks items one by one from the start |
| Binary search | Compares the middle item and discards half the range each time |
| low, high, mid | The ends of the remaining range and its middle position |
| Ordered list | A list sorted in ascending (or descending) order |
| Comparison | One test of an item against the target |
| Not found | Normally signalled by returning -1 |
Linear search
def linear_search(items, target):
for index in range(len(items)):
if items[index] == target:
return index
return -1
- Works on any list, ordered or not.
- Best case: 1 comparison. Worst case: n comparisons.
- Absent target: always n comparisons.
Recomputed on ["Ola", "Maya", "Ravi", "Sam", "Ines", "Tom", "Zoe", "Kai"]:
| Target | Index returned | Comparisons |
|---|---|---|
| “Ines” | 4 | 5 |
| “Ola” | 0 | 1 |
| “Kai” | 7 | 8 |
| “Bea” | -1 | 8 |
Binary search
def binary_search(items, target):
low = 0
high = len(items) - 1
while low <= high:
mid = (low + high) // 2
if items[mid] == target:
return mid
elif items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Rules:
- The list must be ordered.
mid = (low + high) DIV 2.- Middle too small:
low = mid + 1. Middle too large:high = mid - 1. - Stop when found, or when
low > high(not present).
Trace reminder
List [5, 12, 18, 23, 31, 39, 46, 52, 58, 67, 73, 88] (12 items, indexes 0 to 11). Search for 46.
| Pass | low | high | mid | items[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 11 | 5 | 39 | 39 < 46: low = 6 |
| 2 | 6 | 11 | 8 | 58 | 58 > 46: high = 7 |
| 3 | 6 | 7 | 6 | 46 | found, return 6 |
Search for 40 on the same list: mid values are 5 (39), 8 (58) and 6 (46); after pass 3, high becomes 5 and low is 6, so the result is -1.
Comparison
| Linear | Binary | |
|---|---|---|
| Ordered list needed? | No | Yes |
| Worst case comparisons | n | whole part of log base 2 of n, plus 1 |
| Best for | Small or unsorted lists; one-off searches | Large sorted lists; repeated searches |
| Code | Simple | More complex |
| Items | Linear worst | Binary worst |
|---|---|---|
| 12 | 12 | 4 |
| 100 | 100 | 7 |
| 2,000 | 2,000 | 11 |
| 4,000 | 4,000 | 12 |
Doubling the list adds one comparison to the binary worst case. Big O (A-level only): linear O(n), binary O(log n).
Choosing between them
- Is the list ordered? If not, and it will be searched once, use linear.
- Is it small? Linear is simpler and fast enough.
- Is it large, ordered and searched often? Use binary.
- Is sorting needed first? Count the cost of sorting.
Exam traps
- Binary search needs an ordered list. Always state this.
low <= highis the loop condition. Withlow < high, the last item can be missed. For[2, 4, 6, 8, 10]that version returns -1 when searching for 10.mid + 1andmid - 1. Usingmidalone can loop forever.- Absent items. Linear checks every item; binary checks only about log base 2 of n.
- Index versus value. The function returns the index.
- Early stopping helps linear search only when the target is present.
Quick self-test
- What does a linear search return when the target is absent?
- How many comparisons does a linear search of 50 items make when the target is absent?
- What is the difference between the inefficient and advanced linear searches?
- State the condition that must hold for a binary search to be used.
- In a list of 12 items (indexes 0 to 11), what is the first
mid? - What does
lowbecome ifitems[mid]is smaller than the target? - State the loop condition for binary search.
- How does the loop end when the target is absent?
- What is the most comparisons binary search makes on 100 items?
- Why might linear search be chosen for an unsorted list of 20 items searched once?
- Which search does the code
low = mid + 1belong to? - For the list in the trace reminder, search for 5. Which passes occur?
Answers
- -1 (or another agreed “not found” value).
-
- The inefficient version always checks every item; the advanced version stops when the item is found.
- The list must be in order.
- (0 + 11) DIV 2 = 5.
mid + 1.low <= high.lowbecomes greater thanhigh.-
- No sorting is needed, and the list is small.
- Binary search.
- mid 5 (39, too large), mid 2 (18, too large), mid 0 (5, found): index 0 after three passes.
- Searching algorithms study guide
- Searching algorithms practice questions
- Searching and sorting algorithms revision notes
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.4.1 Searching algorithms.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Sorting algorithms
OxfordAQA 9645 sub-topic 3.4.2 in depth: bubble sort and its two improvements, merge sort step by step, and comparing their time and memory use.
Computer Science · OxfordAQA · AS Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Sorting algorithms – Practice Questions
Ten original OxfordAQA 9645 questions on sub-topic 3.4.2: tracing and coding bubble sort, demonstrating merge sort and comparing the two, with answers.
Computer Science · OxfordAQA · AS Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Sorting algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.4.2: bubble sort and its improvements, merge sort, comparing efficiency and a self-test.
Computer Science · OxfordAQA · AS Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.