Skip to content
Menu

Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 16

Sorting: bubble sort

How bubble sort works by comparing and swapping adjacent items, tracing passes, swapping two variables in Python, writing a bubble sort, and its strengths and weaknesses.

Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

Whiteboard video lesson: Sorting: bubble sort

8-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What your child will learn

  • Describe how bubble sort works
  • Trace a bubble sort pass
  • Swap two values in Python
  • Evaluate bubble sort

The method

  1. Compare each pair of neighbouring items.
  2. Swap them if they are in the wrong order.
  3. One pass moves the largest item to the end.
  4. Repeat passes until a pass makes no swaps.

Key words: bubble sort, pass, swap, adjacent, efficiency.

Worked examples

Worked examples from the lesson, with every step.

Question 1

Show the first pass of bubble sort on [4, 2, 6, 1].

  1. Step 1: 4>2 swap → [2, 4, 6, 1]; 4<6 no swap
  2. Step 2: 6>1 swap → [2, 4, 1, 6]

Answer: [2, 4, 1, 6]

Question 2

Continue: second pass on [2, 4, 1, 6].

  1. Step 1: 2<4 no; 4>1 swap → [2, 1, 4, 6]
  2. Step 2: 4<6 no swap

Answer: [2, 1, 4, 6]

Question 3

How many more passes are needed to finish?

  1. Step 1: 2>1 swap → [1, 2, 4, 6]
  2. Step 2: No swaps → stop

Answer: 2 more passes (the last checks no swaps)