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.

Practise this lesson free (7 questions)

Bubble sort, pass by pass

Start at the left. Compare each pair of neighbours and swap them if they are in the wrong order. One sweep along the list is a 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.
4 passes in total: the last one makes no swaps, so the sort stops.
PassList after the passSwaps
Start7, 3, 9, 2, 5-
13, 7, 2, 5, 93
23, 2, 5, 7, 92
32, 3, 5, 7, 91
42, 3, 5, 7, 90

Bubble sort in Python

A flag variable records whether any swap happened. The while loop keeps making passes until a pass makes no swaps.
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

Divide: split the list in half again and again until every list has one item (a list of one item is already sorted).
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.
Merge sort is a divide and conquer algorithm.
StageLists
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?

Merge sort is much faster on large lists, because each merge stage handles every item once and there are only a few stages. It needs extra memory for the smaller lists and is harder to code.
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.
Worked example

Show the first pass of a bubble sort on [4, 1, 3, 2] and count the swaps.

  1. Compare 4 and 1: swap, giving 1, 4, 3, 2.
  2. Compare 4 and 3: swap, giving 1, 3, 4, 2.
  3. Compare 4 and 2: swap, giving 1, 3, 2, 4.

Answer: After pass 1: [1, 3, 2, 4] with 3 swaps.

Key idea

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.

Key words
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 โ†’

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. 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.

  2. 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.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

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? โ†’

Related lessons