← IGCSE Computer Science Paper 2

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Where should Swapped ← FALSE be placed in a bubble sort that stops early?
At the start of each pass, before the inner loopOnce, after the whole sort has finished
Matches
REPEAT FOR i ← 1 TO 4 ... NEXT i Swapped ← FALSE UNTIL Swapped = FALSE
↓
Swapped is reset to FALSE after the pass instead of before it, so it erases any swap that was recorded and the loop always stops after one pass.
Applying
Meanings
Term
UNTIL Swapped = FALSE
Meaning
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.
Practice
REPEAT
Testing
Gaps
Swapped ←
Check Answer
Spots
1DECLARE Swapped : INTEGER
2Swapped ← FALSE

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Giraffe' badge — High Perspective, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Where should Swapped ← FALSE be placed in a bubble sort that stops early?
A. Once, after the whole sort has finished
B. At the start of each pass, before the inner loop ✓
The flag must be reset before every pass. Then it only becomes TRUE if that pass makes a swap.
2. When should Swapped ← TRUE be executed?
A. Whenever two values are swapped ✓
B. Whenever two values are compared
The flag records that a swap happened. A comparison alone does not change the data.
3. Swapped is FALSE at the end of a full pass. What does this tell you?
A. No swaps were needed, so the data is sorted ✓
B. The pass failed, so it must be repeated
If no pair was out of order, the data is already in order and the sort can stop.
4. Which loop ending is correct for a REPEAT version of this bubble sort?
A. UNTIL Swapped = TRUE
B. UNTIL Swapped = FALSE ✓
REPEAT stops when the UNTIL condition is true. We want to stop when a pass has no swaps, so Swapped is FALSE.
5. Which WHILE condition keeps the bubble sort running correctly?
A. WHILE Swapped = TRUE DO ✓
B. WHILE Swapped = FALSE DO
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.

Match the Pairs

Swapped ← FALSE → Reset the flag at the start of each pass, assuming no swaps have happened yet
UNTIL Swapped = FALSE → Stop repeating the passes once a whole pass has made no swaps, so the data is sorted
When two neighbouring values are out of order and get swapped, the flag must record that a swap happened → Swapped ← TRUE
A WHILE version keeps looping as long as the last pass made a swap → WHILE Swapped = TRUE DO
REPEAT FOR i ← 1 TO 4 ... NEXT i Swapped ← FALSE UNTIL Swapped = FALSE → Swapped is reset to FALSE after the pass instead of before it, so it erases any swap that was recorded and the loop always stops after one pass.
IF Numbers[i] > Numbers[i + 1] THEN Temp ← Numbers[i] Numbers[i] ← Numbers[i + 1] Numbers[i + 1] ← Temp ENDIF → Swapped ← TRUE is missing inside the IF, so the flag stays FALSE even when a swap happens and the sort may stop too early.

Key Term

UNTIL Swapped = FALSE
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.

Fill in the Blanks

1. REPEAT Swapped ← FALSE FOR i ← 1 TO 9
Hint: Type the BOOLEAN value the flag is reset to at the start of every pass.
2. IF Numbers[i] > Numbers[i + 1] THEN Swapped ← TRUE ENDIF
Hint: Type the BOOLEAN value that records a swap has just happened.
3. NEXT i UNTIL Swapped = FALSE
Hint: Type the BOOLEAN value that means a whole pass made no swaps, so the loop can end.
4. Swapped ← TRUE WHILE Swapped = TRUE DO Swapped ← FALSE
Hint: Type the BOOLEAN value that lets the WHILE loop run another pass.
5. // Sorted data: no swaps in pass 1 UNTIL Swapped = FALSE OUTPUT "Done"
Hint: Type the name of the BOOLEAN flag that stays FALSE when the data is already sorted.

Spot the Error

Question 1
A DECLARE Swapped : INTEGER
B Swapped ← FALSE
C IF A[1] > A[2] THEN
D Swapped ← TRUE
E ENDIF
Line A — Swapped is a flag that is only ever TRUE or FALSE, so it must be declared as BOOLEAN, not INTEGER.
Question 2
A WHILE
B Swapped ← FALSE
C CALL OnePass(Swapped)
D UNTIL Swapped = FALSE
Line A — The loop ends with UNTIL, so it must open with REPEAT. A WHILE loop would need DO and ENDWHILE.
Question 3
A Swapped ← FALSE
B IF Price[1] > Price[2] THEN
C Swapped = TRUE
D ENDIF
Line C — = is a comparison, not an assignment. To record that a swap happened, use the assignment arrow: Swapped ← TRUE.
Question 4
A REPEAT
B Swapped ← TRUE
C FOR i ← 1 TO 4
D CALL CompareSwap(i, Swapped)
E NEXT i
F UNTIL Swapped = FALSE
Line B — Swapped must be set to FALSE at the start of each pass. If it starts as TRUE, a pass with no swaps still ends TRUE and the loop never stops.
Question 5
A FOR i ← 1 TO 4
B IF A[i] > A[i + 1] THEN
C Swapped ← "TRUE"
D ENDIF
E NEXT i
Line C — Swapped is a BOOLEAN, so it needs the value TRUE without quote marks. "TRUE" is a string, not a BOOLEAN.

Student with a class code? Join your class →