Bubble sort that stops when no swaps are made
A BOOLEAN flag set to FALSE before each pass and to TRUE whenever a swap happens. The bubble sort stops when a full pass leaves it FALSE, meaning no swaps were needed and the data is sorted.
Understanding Bubble sort that stops when no swaps are made in IGCSE Computer Science Paper 2
What it does: Setting Swapped to TRUE inside the swap tells the algorithm that another pass is needed. The flag records that a swap happened. A comparison alone does not change the data. If no pair was out of order, the data is already in order and the sort can stop. WHILE repeats while its condition is true. We keep going while swaps are still being made. Swapped must be set to TRUE before the loop so it starts. The first pass finds nothing out of order. Swapped stays FALSE, so the loop ends.
Common mistakes: Swapped is a flag that is only ever TRUE or FALSE, so it must be declared as BOOLEAN, not INTEGER. The loop ends with UNTIL, so it must open with REPEAT. A WHILE loop would need DO and ENDWHILE.
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
UNTIL Swapped = FALSEFill in the Blanks
Spot the Error
Student with a class code? Join your class →