Sorting: bubble sort

Sorting puts data in order, such as scores from lowest to highest. Bubble sort is the simplest sorting algorithm to follow by hand.

Practise this lesson free (6 questions)

More tools

How bubble sort works

1. Compare the first two items. If they are in the wrong order, swap them.
2. Move along one place and compare the next pair. Keep going to the end of the list. That is one pass.
3. After each pass, the biggest unsorted item has 'bubbled' to the end.
4. Repeat passes until a whole pass makes no swaps. Then the list is sorted.

One pass on 5, 3, 8, 1, 4

5 and 3: swap. 5 and 8: no swap. 8 and 1: swap. 8 and 4: swap.
After pass 1: 3, 5, 1, 4, 8. The 8 is now in its final place.
After pass 2: 3, 1, 4, 5, 8.

Good and bad points

Bubble sort is easy to understand and code, and sorts the list in place without extra memory.
But it is slow on big lists, because it makes many comparisons and can need many passes.
For large lists, faster algorithms such as merge sort are used.
Worked example

Show the list 6, 2, 9, 4 after the first pass of bubble sort.

  1. 6 and 2: swap: 2, 6, 9, 4.
  2. 6 and 9: no swap.
  3. 9 and 4: swap: 2, 6, 4, 9.

Answer: 2, 6, 4, 9

Key idea

Bubble sort compares neighbouring pairs and swaps them if they are in the wrong order. Each pass moves the largest unsorted item to the end. It stops after a pass with no swaps. Simple, but slow for big lists.

Key words
Bubble sort
A sort that compares neighbouring pairs and swaps them if they are in the wrong order, making passes until no swaps are needed.
Algorithm
A precise set of step-by-step instructions to solve a problem or complete a task.
AND
A Boolean operator that gives True (1) only when both inputs are True.
List
A data structure that stores many items under one name, in order, such as scores = [4, 7, 2].
Merge sort
A divide and conquer sort that splits a list into single items, then merges them back together in order.

All KS3 Computing 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. Write 3, 4, 1, 2 after one pass of bubble sort (ascending). Separate the numbers with commas.

    Show the answer

    Answer: 3, 1, 2, 4

    3,4: no swap; 4,1: swap; 4,2: swap. Result: 3, 1, 2, 4.

  2. Write 8, 3, 6, 1, 7 after one pass of bubble sort (ascending). Separate the numbers with commas.

    Show the answer

    Answer: 3, 6, 1, 7, 8

    8,3: swap; 8,6: swap; 8,1: swap; 8,7: swap. Result: 3, 6, 1, 7, 8.

Check you have got it

Answer 6 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

Facts checked against the sources below, 27 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.