← IGCSE Computer Science Paper 2

One pass of a bubble sort

One pass: a FOR loop compares each item with its neighbour and swaps them if the first is larger, so the biggest value moves to the end.

Understanding One pass of a bubble sort in IGCSE Computer Science Paper 2

What it does: Using < instead of > swaps pairs when the first is smaller, so the smallest value is pushed to the end and the list ends up in descending order. A pass compares an item with the NEXT one, Marks[Index + 1]. Comparing Marks[Index] with itself never swaps anything. A swap only happens if Marks[Index] > Marks[Index + 1]. Putting it after ENDIF would swap every pair, even ones already in order. One pass compares 5,9 (no swap), then 9,2 (swap), then 9,7 (swap). The biggest value, 9, moves to the end, but the array is not yet fully sorted. The comparison uses Index + 1, so the final Index must be n - 1 to keep the neighbour inside the array.

Common mistakes: Num[i] is compared with itself, so the condition is never true and nothing is ever swapped. It should compare with the neighbour: IF Num[i] > Num[i + 1] THEN. This puts the old Num[i] back where it started, so Num[i + 1] never changes and the swap fails. The last swap line should be Num[i + 1] ← Temp.

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
In one pass of a bubble sort, which comparison checks each item against its neighbour?
IF Marks[Index] > Marks[Index] THENIF Marks[Index] > Marks[Index + 1] THEN
Matches
FOR Index ← 1 TO n IF Marks[Index] > Marks[Index + 1] THEN
↓
The loop goes up to n, so on the last turn Marks[Index + 1] is outside the array and causes an error. The upper bound should be n - 1.
Applying
Meanings
Term
IF Marks[Index] > Marks[Index + 1] THEN
Meaning
One pass: a FOR loop compares each item with its neighbour and swaps them if the first is larger, so the biggest value moves to the end.
Practice
FOR Index ← 1 TO 4
Testing
Gaps
IF Marks[Index] Marks[Index + 1] THEN
Check Answer
Spots
1FOR i ← 1 TO n - 1
2 IF Num[i] > Num[i] THEN

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

The 'Badger' badge — Determined Digger, 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. In one pass of a bubble sort, which comparison checks each item against its neighbour?
A. IF Marks[Index] > Marks[Index] THEN
B. IF Marks[Index] > Marks[Index + 1] THEN ✓
A pass compares an item with the NEXT one, Marks[Index + 1]. Comparing Marks[Index] with itself never swaps anything.
2. An array Marks has 6 items, so n = 6. Which loop header is correct for one pass?
A. FOR Index ← 1 TO n - 1 ✓
B. FOR Index ← 1 TO n
The loop stops at n - 1 so that Index + 1 is never larger than n. With TO n, Marks[Index + 1] would be outside the array on the last loop.
3. Where should the three swap lines go in one pass of a bubble sort?
A. Inside the IF, so they only run when the pair is out of order ✓
B. After ENDIF, so they run on every loop
A swap only happens if Marks[Index] > Marks[Index + 1]. Putting it after ENDIF would swap every pair, even ones already in order.
4. The array Marks holds 5, 9, 2, 7. After one full pass (ascending order), what does it hold?
A. 2, 5, 7, 9
B. 5, 2, 7, 9 ✓
One pass compares 5,9 (no swap), then 9,2 (swap), then 9,7 (swap). The biggest value, 9, moves to the end, but the array is not yet fully sorted.
5. What is guaranteed after one complete pass of a bubble sort (ascending)?
A. The largest value is now at the end of the array ✓
B. The whole array is now in order
One pass moves the biggest value to the end. More passes are usually needed to sort the rest.

Match the Pairs

IF Marks[Index] > Marks[Index + 1] THEN → Compares an item with its neighbour and runs the swap only if the first is larger than the second
FOR Index ← 1 TO n - 1 → Loops so that Index + 1 never goes past the last item in the array
To sort into descending order, change the comparison so the swap happens when the first item is smaller than its neighbour → IF Marks[Index] < Marks[Index + 1] THEN
The swap must be inside the IF, so it only happens when the pair is in the wrong order → IF Marks[Index] > Marks[Index + 1] THEN Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp ENDIF
FOR Index ← 1 TO n IF Marks[Index] > Marks[Index + 1] THEN → The loop goes up to n, so on the last turn Marks[Index + 1] is outside the array and causes an error. The upper bound should be n - 1.
IF Marks[Index] > Marks[Index + 1] THEN Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index] ENDIF → There is no temporary variable, so the first assignment overwrites Marks[Index] and both items end up with the same value. Use Temp to hold one value first.

Key Term

IF Marks[Index] > Marks[Index + 1] THEN
One pass: a FOR loop compares each item with its neighbour and swaps them if the first is larger, so the biggest value moves to the end.

Fill in the Blanks

1. FOR Index ← 1 TO 4 IF Marks[Index] > Marks[Index + 1] THEN ENDIF
Hint: Type the comparison operator that checks whether the first item is larger than its neighbour.
2. FOR Index ← 1 TO n - 1 IF Marks[Index] > Marks[Index + 1] THEN ENDIF
Hint: Type the expression for the last Index value, so that Index + 1 never goes past the end of the array.
3. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the name of the temporary variable that saves the first value before it is overwritten.
4. IF Marks[Index] < Marks[Index + 1] THEN Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1]
Hint: Type the operator that makes the pass sort into descending order, moving the smallest value to the end.
5. Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp ENDIF
Hint: Type the index of the neighbouring element that receives the saved value to complete the swap.

Spot the Error

Question 1
A FOR i ← 1 TO n - 1
B IF Num[i] > Num[i] THEN
C SWAP(Num[i], Num[i + 1])
D ENDIF
E NEXT i
Line B — Num[i] is compared with itself, so the condition is never true and nothing is ever swapped. It should compare with the neighbour: IF Num[i] > Num[i + 1] THEN.
Question 2
A IF Num[i] > Num[i + 1] THEN
B Temp ← Num[i]
C Num[i] ← Num[i + 1]
D Num[i] ← Temp
E ENDIF
Line D — This puts the old Num[i] back where it started, so Num[i + 1] never changes and the swap fails. The last swap line should be Num[i + 1] ← Temp.
Question 3
A // One pass, ascending
B FOR i ← 1 TO n
C IF Num[i] > Num[i + 1] THEN
D SWAP(Num[i], Num[i + 1])
E ENDIF
F NEXT i
Line B — When i reaches n, Num[i + 1] is outside the array. The upper bound must be n - 1 so that i + 1 stays inside the array.
Question 4
A // One pass, descending
B FOR i ← 1 TO n - 1
C IF Num[i] > Num[i + 1] THEN
D SWAP(Num[i], Num[i + 1])
E ENDIF
F NEXT i
Line C — Using > swaps larger values towards the end, which gives ascending order. For descending order the test must be <: IF Num[i] < Num[i + 1] THEN.
Question 5
A FOR i ← 1 TO n - 2
B IF Num[i] > Num[i + 1] THEN
C SWAP(Num[i], Num[i + 1])
D ENDIF
E NEXT i
Line A — The loop stops one pair too early, so the last two items are never compared and the biggest value may not reach the end. The upper bound should be n - 1.

Student with a class code? Join your class →