AQA: a linear search is an algorithm that sequentially checks each element in an array or list until a match is found or the end is reached.
Start at the first item. Compare it with the item you are looking for (the target).
If they match, stop: the item is found. If not, move to the next item.
If you reach the end without a match, the item isn’t in the list.
It works on unsorted lists as well as sorted ones.
Best case: the target is first, so 1 comparison. Worst case: it is last or not there at all, so one comparison per item (n for a list of n items).
AQA pseudo-code
items ← [14, 3, 27, 8, 19, 5]
target ← USERINPUT
found ← False
i ← 0
WHILE found = False AND i < LEN(items)
IF items[i] = target THEN
found ← True
ELSE
i ← i + 1
ENDIF
ENDWHILE
A linear search checks one item at a time, from the start
Exam tipA linear search does not need a sorted list: examiners see that mistake every year. And remember the item might not be there at all.
Linear search
An algorithm that sequentially checks each element in an array or list until a match is found or the end is reached.
Target
The item being searched for.
3.1.3 Searching algorithms
Binary search
AQA: a binary search compares a search item to the middle item of an array or list. If they do not match, half of the array or list can be eliminated and the search continues in the other half. The array or list needs to be sorted.
Set low to the first index and high to the last.
Find the middle: mid ← (low + high) DIV 2. Compare the item at mid with the target.
Equal: found. Middle item smaller than the target: the target must be to the right, so low ← mid + 1. Larger: it must be to the left, so high ← mid - 1.
Repeat. If low becomes greater than high, there is nothing left to check, so the item isn’t there.
Strings can be binary searched too, if they are in alphabetical order.
AQA pseudo-code (items is sorted; target has been input)
low ← 0
high ← LEN(items) - 1
found ← False
WHILE found = False AND low ≤ high
mid ← (low + high) DIV 2
IF items[mid] = target THEN
found ← True
ELSE IF items[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
ENDIF
ENDWHILE
Finding 30: each comparison throws away the half that can’t hold it
Worked exampleBinary search for 30 in [2, 5, 9, 12, 16, 21, 25, 30, 34, 40, 47] (indexes 0 to 10).
low 0, high 10, mid (0 + 10) DIV 2 = 5: items[5] = 21. 21 < 30, so low = 630 must be to the right
low 6, high 10, mid 8: items[8] = 34. 34 > 30, so high = 730 must be to the left of 34
low 6, high 7, mid (6 + 7) DIV 2 = 6: items[6] = 25. 25 < 30, so low = 7DIV rounds down
low 7, high 7, mid 7: items[7] = 30. Founda match
Found at index 7 after 4 comparisons (21, 34, 25, 30). A linear search would need 8.
Exam tipOnly about half of students could say why binary search wouldn’t work on a list: it needs the list to be sorted. It works on words in alphabetical order as well as numbers.
Binary search
An algorithm that compares a search item to the middle item of an array or list; if they don’t match, half of the list is eliminated and the search continues in the other half. The list must be sorted.
3.1.3 Searching algorithms
Comparing linear and binary search
If a list changes often, keeping it sorted for binary search costs time. A linear search can be used straight away.
For a short list, the difference in time is tiny, so the simpler linear search is fine.
Linear search
Binary search
Does the list need sorting?
No: works on unsorted data
Yes: must be sorted first
Comparisons on a large list
Up to one per item
Far fewer: each comparison removes about half of what’s left
Coding
Simpler to write
More complex (low, high and mid to track)
Best for
Small or unsorted lists, or lists that change often
Large sorted lists that are searched often
Items in a sorted list
Linear search: most comparisons
Binary search: most comparisons
10
10
4
100
100
7
1000
1000
10
1 000 000
1 000 000
20
Exam tipDon’t just write “binary search is faster”. Say why: “binary search makes fewer comparisons on a large sorted list, because each comparison halves the items left, so it takes less time.”
3.1.4 Sorting algorithms
Bubble sort
AQA: a bubble sort works by making passes through the array or list. Whilst making a pass, we compare neighbours and swap them if they are out of order. After a series of passes, the array or list will be sorted.
A pass compares items 0 and 1, then 1 and 2, and so on to the end.
If a pair is out of order, swap them. A swap needs a temporary variable, so one value isn’t overwritten before it is copied.
After the first pass (ascending order), the largest item is in its final place at the end. After each pass, one more item is in place.
Stopping: a list of n items is sorted after at most n − 1 passes. Most versions stop as soon as a pass makes no swaps, because then the list must be in order.
AQA pseudo-code: bubble sort with an early stop
swapped ← True
WHILE swapped = True
swapped ← False
FOR i ← 0 TO LEN(items) - 2
IF items[i] > items[i + 1] THEN
temp ← items[i]
items[i] ← items[i + 1]
items[i + 1] ← temp
swapped ← True
ENDIF
ENDFOR
ENDWHILE
The first pass: the largest item “bubbles” to the end
Bubble sort
A sort that makes passes through a list, comparing neighbours and swapping them if they are out of order, until the list is sorted.
Pass
One run through the list, comparing each pair of neighbours once.
Swap
Exchanging two items, using a temporary variable to hold one of them.
3.1.4 Sorting algorithms
Showing the steps of a bubble sort
AQA often asks you to show the list each time it changes, which means after every swap, not just after every pass.
Worked exampleBubble sort [5, 1, 4, 2] into ascending order. Show the list after each swap.
Pass 1: 5 > 1 swap → [1, 5, 4, 2]; 5 > 4 swap → [1, 4, 5, 2]; 5 > 2 swap → [1, 4, 2, 5]three swaps; 5 is now in place
Worked exampleSort ['owl', 'bat', 'elk'] into alphabetical order with a bubble sort. Show each change.
'owl' > 'bat' alphabetically: swap → ['bat', 'owl', 'elk']b comes before o
'owl' > 'elk': swap → ['bat', 'elk', 'owl']e comes before o
Pass 2: 'bat' < 'elk', 'elk' < 'owl': no swapssorted
Two changes. Words compare letter by letter, from the first letter.
Exam tipIn June 2025, about a third of students missed full marks here, often by showing only the end of each pass. Write a new line for every swap.
3.1.4 Sorting algorithms
Merge sort
AQA: the merge sort is a ‘divide-and-conquer’ algorithm. The original array or list is broken down into smaller and smaller parts until we have individual elements. The merge sort continues by building back sorted groups until we end with the full sorted result.
Split the list in half, then split each half, until every list holds one item. A list of one item is already sorted.
Merge pairs of lists back together in order. Keep merging until there is one list.
Merging and sorting are the same step. Each merge produces a sorted list, because items are taken in order.
If a list has an odd number of items, one half gets one more item than the other.
Worked exampleMerge the sorted lists [4, 9, 15] and [6, 7, 20].
Fronts 4 and 6: take 4 → [4]always compare the two front items
9 and 6: take 6 → [4, 6]. 9 and 7: take 7 → [4, 6, 7]take the smaller each time
9 and 20: take 9. 15 and 20: take 15 → [4, 6, 7, 9, 15]the first list is now empty
Copy the rest of the other list: 20nothing left to compare
[4, 6, 7, 9, 15, 20]
Merge sort
A divide-and-conquer sort: the list is broken down until there are individual elements, then sorted groups are built back up until the whole list is sorted.
Merge
Combine two sorted lists into one sorted list by repeatedly taking the smaller front item.
Divide and conquer
Solving a problem by splitting it into smaller problems, solving those and combining the results.
3.1.4 Sorting algorithms
Drawing a merge sort
Draw a new row for each round of splitting, until every box holds one item.
Then draw a new row for each round of merging. Each merge joins two neighbouring lists from the row above, in order.
A list of 8 items takes 3 rounds of splitting and 3 rounds of merging.
Split down to single items, then merge back up in order
Worked exampleShow the stages of a merge sort on [31, 12, 8, 25, 17, 4].
Merge: [8, 12] and [4, 17]; then [8, 12, 31] and [4, 17, 25]merge back in the reverse order of splitting
Merge: [4, 8, 12, 17, 25, 31]the last merge
[4, 8, 12, 17, 25, 31]
Exam tipFewer than 40% of students got full marks for completing a merge diagram in June 2025. Many treated merging and sorting as two separate steps, merging lists without putting the items in order.
3.1.4 Sorting algorithms
Comparing merge sort and bubble sort
For a large, unsorted list, merge sort is the better choice: it takes far less time.
For a small or nearly sorted list, or where memory is tight, bubble sort can be the better choice.
Bubble sort
Merge sort
Time on a large list
Slow: at least 499 500 comparisons for 1000 items in reverse order
Much quicker: fewer than 10 000 comparisons for 1000 items
Nearly sorted list
Quick: with the early stop, one pass can be enough
Still splits and merges everything
Memory
Sorts in place: needs only one extra variable for swaps
Needs extra memory for the lists it builds while merging
Coding
Simple to write
More complex to write
Exam tip“Bubble sort is simple” isn’t enough. Qualify it: “bubble sort is simpler to code” or “bubble sort uses less memory because it sorts in place”.
3.1.3 Searching algorithms
Writing a linear search in Python
Paper 1 may ask you to write your own search. When it says so, don’t use Python’s built-in shortcuts such as if target in names or names.index(target).
i starts at 0, and i < len(names) stops the loop before it runs off the end.
The loop stops as soon as the name is found, or when every item has been checked.
The result is output once, after the loop.
Python: a linear search that stops when it finds a match
names = ["Ola", "Kit", "Raj", "Bea", "Tom"]
target = input("Name to find: ")
found = False
i = 0
while i < len(names) and not found:
if names[i] == target:
found = True
else:
i = i + 1
if found:
print(f"{target} is at position {i}")
else:
print(f"{target} is not in the list")
Exam tipThe commonest errors: printing “not found” inside the loop (so it appears once per item), indexes that run past the end, and using in or index() when the question asked for your own search.