Sort a list with bubble sort
Compare adjacent values and swap out-of-order pairs over repeated passes.
This sequence follows Cambridge IGCSE Computer Science 0478, 2026–2028 syllabus Version 6. It covers subject content Topic 7 only. The separately published 2029–2031 syllabus is not mapped here. Practice is original, self-checked and does not award an official Cambridge grade. Full timed papers and human subject-teacher review remain outstanding.
Before you begin
- No programming language is required. Be comfortable with place value and multiplication; each lesson introduces the computer-science notation it uses.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
In ascending bubble sort, what happens when left value is greater than right value?
What you will learn
- Describe a bubble-sort pass
- Trace adjacent comparisons and swaps
- Explain when repeated passes can stop
Bubble sort moves values by adjacent swaps
Bubble sort compares neighbouring items in a list. If a pair is in the wrong order, it swaps them; after a pass, a large value has moved toward the end of an ascending list.
Apply and check the rule
Repeat passes until the list is sorted. A program may stop after a pass with no swaps or after a known number of passes. Use a temporary variable or safe swap method so neither value is lost.
Pause and explain
After one ascending pass on [7, 3, 5], what is the list?
Worked example
Perform one left-to-right ascending bubble-sort pass on [5, 2, 4].
Show the worked solution
- Compare 5 and 2; swap to get [2, 5, 4].
- Compare 5 and 4; swap to get [2, 4, 5].
- The list after one pass is [2, 4, 5].
Answer [2, 4, 5].
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Make one ascending bubble-sort pass over [6, 1, 4, 2].
Give me a hint
Compare each neighbouring pair from left to right.
Compare my reasoning
- Swap 6 and 1: [1, 6, 4, 2].
- Swap 6 and 4: [1, 4, 6, 2].
- Swap 6 and 2: [1, 4, 2, 6].
After one pass: [1, 4, 2, 6].
Look for these in your work
- Adjacent comparisons
- Swaps
- Final pass state
A bubble-sort routine swaps when Left < Right but claims to sort ascending. What order will it tend to produce?
Give me a hint
For ascending order, larger values should move right.
Compare my reasoning
- If left is smaller, the pair is already in ascending order.
- Swapping such a pair moves smaller values rightward.
- The condition is reversed for an ascending sort.
It tends to move smaller values to the right, producing descending order; swap when Left > Right instead.
Look for these in your work
- Condition interpreted
- Direction
- Correction
A sort stops after one pass even though a swap occurred. Explain how an early-stop optimisation should work.
Give me a hint
A pass with swaps means the list may still be unsorted.
Compare my reasoning
- Track whether any pair was swapped during the pass.
- If a swap occurred, another pass may be needed.
- Stop early only after a complete pass with no swaps.
Use a Swapped flag: reset it before a pass, set it on any swap, and stop only when an entire pass has no swaps.
Look for these in your work
- Flag reset
- Flag set
- Stop condition
Common mistakes
- Comparing non-adjacent items as if they were a bubble-sort pair
- Swapping values without preserving one of them
What can you explain now?
Which values does bubble sort compare at each step?
Compare with the explanation
Adjacent values
It compares neighbours and swaps a pair if they are out of order.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
What happens during a bubble-sort pass, and when can the algorithm stop early?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.