GCSE · Computer Science · AQA · Spec 8525

Bubble sort

Bubble sort only ever sees two neighbours at a time, so how does it know when it's finished?

Watch it work

Bubble sort, one comparison at a time

Four numbers, three passes. Step through every comparison and watch the list change.

Before each step, call it: swap or no swap?

PassComparingSwap?List now
3, 1, 4, 2

Output

 

Step 1: Here's our unsorted list. We want it in ascending order, smallest first. Bubble sort starts at the left and only ever looks at two neighbours at a time.

1 / 13

Press Next to go one step at a time, Play to watch it run, or Back to rewind.

Step 1 of 13: Here's our unsorted list. We want it in ascending order, smallest first. Bubble sort starts at the left and only ever looks at two neighbours at a time..

Watch out: Keep an eye on step 7. Something happens there that the algorithm itself can't notice.

Predict, then check

Same rules, new list. Commit to an answer before you look.

Bubble sort is putting 2, 1, 3, 4 into ascending order. Its very first swap turns the list into 1, 2, 3, 4. What does the algorithm do next?

Your turn

Write the list after each pass

Use bubble sort to put 4, 9, 2, 7, 5 into ascending order. Write down the list after each pass.

  1. Start: 4, 9, 2, 7, 5Pass 1 compares 4 and 9, then 9 and 2, then 9 and 7, then 9 and 5.
  2. missing step
Which line is step 2?

Put it in words

Explain how bubble sort works

Explain how the bubble sort algorithm puts a list of numbers into ascending order. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Compare two neighbours, swap them if they're the wrong way round, and keep going until nothing moves.

What you need to know

  • Bubble sort works by going through a list again and again.
  • It compares adjacent items (two items next to each other) and swaps them if they are in the wrong order.
  • 'Wrong order' depends on the order you want: for ascending order (smallest first), swap when the left item is bigger than the right one.
  • Each time the algorithm goes through the list is called a pass.
  • Passes are repeated until a pass makes no changes. That last pass is needed to confirm the data is in order.

The big picture

Bubble sort puts a list in order by going through it again and again. On each trip through, called a pass, it compares adjacent items and swaps any pair that is in the wrong order. It keeps making passes until one pass makes no changes at all. That last pass matters: because the algorithm only ever sees two items at a time, a change-free pass is how it confirms the list is in order.

Key points

1Neighbours only: bubble sort never compares or swaps two items that aren't next to each other.
2A pass always runs to the end of the list, even if the list becomes sorted partway through.
3Any swap on a pass means go again. No swaps on a pass means stop.
4A list can be in order before the algorithm has finished. It still needs one change-free pass to be sure.
5In an ascending sort, the largest value has reached the end of the list after the first pass.

Worked example

Problem

Use bubble sort to put pear, apple, kiwi, fig into alphabetical order. Show every comparison.

⚠ Watch out

Stopping the moment the list looks sorted. Bubble sort only stops after a pass with no swaps, so its final pass never changes anything: it's there to check.

🧠

Memory hook

Picture a teacher lining students up by height who is only allowed to compare two neighbours at a time. They can't say the line is sorted until they've walked the whole line once without having to move anybody.

✓

Check yourself

A list of five numbers is already in ascending order before bubble sort starts. How many comparisons will the algorithm make before it stops, and why?

Flashcards

(8)
What does bubble sort compare?
Adjacent items: two items that are next to each other in the list.
When does bubble sort swap two items?
When they're in the wrong order for the sort. For ascending order, that's when the left item is bigger than the right one.
In bubble sort, what is a pass?
One time through the list, from the start to the end.
When does bubble sort stop?
After a pass in which no changes (no swaps) are made.
Why is the last pass needed?
To confirm the data is in order. The algorithm only compares neighbours, so it can't know the list is sorted until a whole pass makes no changes.
A pass makes just one swap. What happens next?
Another pass. Any change means something might still be out of order.
If the list becomes sorted partway through a pass, does the pass stop there?
No. A pass always carries on to the end of the list, comparing every pair of neighbours.
In an ascending bubble sort, where is the largest value after pass 1?
At the end of the list. Once the pass reaches it, it's bigger than each neighbour after it, so it keeps getting swapped along.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 8 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.