Bubble sort and merge sort step by step
Exam questions ask you to show each pass of a bubble sort or each stage of a merge sort. This lesson takes both algorithms slowly, with real code.
Bubble sort, pass by pass
After pass 1 the largest item has 'bubbled' to the end; after pass 2 the two largest are in place, and so on.
The algorithm stops after a pass in which no swaps happen. That final pass is needed to prove the list is sorted.
| Pass | List after the pass | Swaps |
|---|---|---|
| Start | 7, 3, 9, 2, 5 | - |
| 1 | 3, 7, 2, 5, 9 | 3 |
| 2 | 3, 2, 5, 7, 9 | 2 |
| 3 | 2, 3, 5, 7, 9 | 1 |
| 4 | 2, 3, 5, 7, 9 | 0 |
Bubble sort in Python
numbers = [7, 3, 9, 2, 5]
swapped = True
while swapped:
swapped = False
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i + 1]:
temp = numbers[i]
numbers[i] = numbers[i + 1]
numbers[i + 1] = temp
swapped = True
print(numbers)
Output: [2, 3, 5, 7, 9]
With n items, each pass makes n - 1 comparisons. Bubble sort is simple and needs almost no extra memory, but on a large, jumbled list it makes a very large number of comparisons.
Merge sort: divide, then merge
Merge: join pairs of lists. Compare the front items of the two lists, move the smaller one into the new list, and repeat until both are empty.
| Stage | Lists |
|---|---|
| Start | [6, 2, 8, 4, 5, 1, 7, 3] |
| Split into single items | [6] [2] [8] [4] [5] [1] [7] [3] |
| Merge into pairs | [2, 6] [4, 8] [1, 5] [3, 7] |
| Merge into fours | [2, 4, 6, 8] [1, 3, 5, 7] |
| Final merge | [1, 2, 3, 4, 5, 6, 7, 8] |
Which is better?
Bubble sort is easy to code and uses very little memory. It is fine for small lists, or lists that are almost sorted, where it may finish after one or two passes.
Show the first pass of a bubble sort on [4, 1, 3, 2] and count the swaps.
- Compare 4 and 1: swap, giving 1, 4, 3, 2.
- Compare 4 and 3: swap, giving 1, 3, 4, 2.
- Compare 4 and 2: swap, giving 1, 3, 2, 4.
Answer: After pass 1: [1, 3, 2, 4] with 3 swaps.
Bubble sort compares neighbours and swaps them, pass after pass, until a pass makes no swaps. Merge sort splits a list into single items, then merges pairs in order. Merge sort is faster on big lists but uses more memory.
- Bubble sort
- Sorts by comparing neighbouring items and swapping them, in passes, until a pass makes no swaps.
- Merge sort
- Splits a list into single items, then merges pairs of lists back together in order.
- Algorithm
- A precise sequence of steps to solve a problem.
- Variable
- A named store whose value can change while the program 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.
How many swaps are made in the FIRST pass of a bubble sort on [6, 2, 7, 4]?
Show the answer
Answer: 2
6,2: swap; 6,7: no swap; 7,4: swap. That is 2 swaps.
Show [8, 3, 6, 2, 5] after TWO passes of a bubble sort. Type it like 1, 2, 3, 4, 5.
Show the answer
Answer: 3, 2, 5, 6, 8
After pass 1: 3, 6, 2, 5, 8. After pass 2: 3, 2, 5, 6, 8.
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
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 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? โ