Bubble sort: how it works
A sorting method that compares neighbouring items and swaps them if in the wrong order, repeating passes until the list is sorted. The largest value bubbles to the end each pass.
Understanding Bubble sort: how it works in IGCSE Computer Science Paper 2
What it does: A FOR loop steps through the neighbouring pairs in one pass. With 5 items there are 4 pairs, so i runs from 1 to 4. Descending order puts the largest values first. So a pair is in the wrong order if the left value is smaller than the right one, and the comparison uses < instead of >. A bubble sort always compares two items that sit side by side, then moves along one position and compares the next pair. Each pass fixes the next largest value at the end. A list of n items may need several passes before every item is in place.
Common mistakes: Line 3 should be A[i] ← A[i + 1]. A swap needs Temp to hold the first value, then the second value moves into position i, then Temp goes into position i + 1. As written, A[i] gets its own value back and the value in A[i + 1] is never moved. For ascending order, neighbours are in the wrong order when the first is bigger than the second, so the test must be A[i] > A[i + 1]. Using < swaps pairs that are already correct.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 296–306) and word search (Modules 296–316).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
bubble sortFill in the Blanks
Spot the Error
Student with a class code? Join your class →