← IGCSE Computer Science Paper 2

Bubble sort in pseudocode

A sort that makes repeated passes, comparing neighbouring items and swapping them using a Temp variable if they are in the wrong order, until a pass makes no swaps.

Understanding Bubble sort in pseudocode in IGCSE Computer Science Paper 2

What it does: Each pair uses Item[i + 1], so the last value of i must be Count - 1. Going to Count would read past the end of the array. Swapped is set to FALSE at the start of each pass. If the pass never sets it to TRUE, the array is sorted and the REPEAT loop ends. Without Temp, the first assignment overwrites Item[i], so both elements end up with the same value. Temp stores the old value first. The outer loop controls the number of passes. The inner loop does the comparing, and the swap happens inside an IF statement within it.

Common mistakes: The FOR loop must set its counter with the assignment arrow, so it should be FOR i ← 0 TO 3. In pseudocode = is used for comparison, not assignment. A DECLARE statement needs a colon between the identifier and its data type. It should be DECLARE Temp : INTEGER.

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 242–253) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which pseudocode correctly swaps Item[i] and Item[i + 1] in a bubble sort?
Item[i] ← Item[i + 1] Item[i + 1] ← Item[i]Temp ← Item[i] Item[i] ← Item[i + 1] Item[i + 1] ← Temp
Matches
Item[i] ← Item[i + 1] Item[i + 1] ← Item[i]
↓
No Temp variable is used, so Item[i] is overwritten and both elements end up with the same value. The original Item[i] is lost.
Applying
Meanings
Term
bubble sort
Meaning
A sort that makes repeated passes, comparing neighbouring items and swapping them using a Temp variable if they are in the wrong order, until a pass makes no swaps.
Practice
FOR Pass ← 1 TO 4
Testing
Gaps
← Item[i]
Check Answer
Spots
1FOR i = 0 TO 3
2 IF Item[i] > Item[i + 1] THEN

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

The 'Elephant' badge — OOP Heavyweight, 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. Which pseudocode correctly swaps Item[i] and Item[i + 1] in a bubble sort?
A. Item[i] ← Item[i + 1] Item[i + 1] ← Item[i]
B. Temp ← Item[i] Item[i] ← Item[i + 1] Item[i + 1] ← Temp ✓
Without Temp, the first assignment overwrites Item[i], so both elements end up with the same value. Temp stores the old value first.
2. What does the inner loop of a bubble sort compare?
A. The first element with every other element in the array
B. Neighbouring elements: Item[i] and Item[i + 1] ✓
Bubble sort compares each pair of neighbouring items. Comparing the first item with all the others is a different kind of sort.
3. When should a bubble sort swap Item[i] and Item[i + 1] to sort into ascending order?
A. IF Item[i] > Item[i + 1] THEN ✓
B. IF Item[i] < Item[i + 1] THEN
For ascending order, a swap is needed when the left item is bigger than the right item. Using < would sort the array into descending order.
4. True or false: a bubble sort can stop when a complete pass makes no swaps.
A. True: no swaps means every neighbouring pair is already in order ✓
B. False: it must always keep making passes until the loop counter ends
If no swaps happen in a pass, the array is sorted. A flag such as Swapped ← FALSE can be used to check this.
5. What is the job of the outer loop in a bubble sort?
A. It repeats the passes through the array ✓
B. It swaps the two items using Temp
The outer loop controls the number of passes. The inner loop does the comparing, and the swap happens inside an IF statement within it.

Match the Pairs

Temp ← Item[i] Item[i] ← Item[i + 1] Item[i + 1] ← Temp → Swaps two neighbouring elements by holding one value in a temporary variable first
IF Item[i] > Item[i + 1] THEN → Compares two neighbouring elements and checks whether they are in the wrong order for an ascending sort
The inner loop visits every neighbouring pair in one pass, so it stops one place before the end of the array → FOR i ← 1 TO Count - 1
The outer loop repeats passes until a pass makes no swaps, using a flag to record whether a swap happened → REPEAT Swapped ← FALSE // inner loop pass UNTIL Swapped = FALSE
Item[i] ← Item[i + 1] Item[i + 1] ← Item[i] → No Temp variable is used, so Item[i] is overwritten and both elements end up with the same value. The original Item[i] is lost.
FOR i ← 1 TO Count IF Item[i] > Item[i + 1] THEN → The loop goes up to Count, so on the last pass Item[i + 1] is outside the array. The loop should end at Count - 1.

Key Term

bubble sort
A sort that makes repeated passes, comparing neighbouring items and swapping them using a Temp variable if they are in the wrong order, until a pass makes no swaps.

Fill in the Blanks

1. Temp ← Item[i] Item[i] ← Item[i + 1] Item[i + 1] ← Temp
Hint: Type the name of the variable that holds one value safely while the two items are swapped.
2. IF Item[i] > Item[i + 1] THEN // swap the two items ENDIF
Hint: Type the relational operator that checks whether the left item is bigger, so the pair is in the wrong order for an ascending sort.
3. FOR i ← 1 TO 9 // compare Item[i] and Item[i + 1] NEXT i
Hint: Type the final value of i. The array has 10 items numbered 1 to 10, and the loop also looks one place ahead of i.
4. REPEAT // inner loop makes one pass UNTIL Swapped = FALSE
Hint: Type the Boolean value that Swapped holds when a whole pass made no swaps, so the outer loop can stop.
5. IF Item[i] > Item[i + 1] THEN Swapped ← TRUE ENDIF
Hint: Type the Boolean value that records that a swap happened, so another pass is needed.

Spot the Error

Question 1
A FOR i = 0 TO 3
B IF Item[i] > Item[i + 1] THEN
C Temp ← Item[i]
D Item[i] ← Item[i + 1]
E Item[i + 1] ← Temp
F ENDIF
Line A — The FOR loop must set its counter with the assignment arrow, so it should be FOR i ← 0 TO 3. In pseudocode = is used for comparison, not assignment.
Question 2
A DECLARE Temp INTEGER
B Temp ← Item[i]
C Item[i] ← Item[i + 1]
D Item[i + 1] ← Temp
Line A — A DECLARE statement needs a colon between the identifier and its data type. It should be DECLARE Temp : INTEGER.
Question 3
A IF Item[i] > Item[i + 1] THEN
B Temp ← Item[i]
C Item[i] ← Item[i + 1]
D Item[i + 1] ← Item[i]
E ENDIF
Line D — The second item must receive the value saved in Temp. Copying Item[i] again means the original Item[i] value is lost and Temp is never used. It should be Item[i + 1] ← Temp.
Question 4
A FOR i ← 0 TO 3
B IF Item[i] > Item[i + 1] THEN
C Temp ← Item[i + 1]
D Item[i] ← Item[i + 1]
E Item[i + 1] ← Temp
F ENDIF
Line C — Temp must store the value of Item[i] before it is overwritten. Storing Item[i + 1] means the old Item[i] value is lost and the swap fails. It should be Temp ← Item[i].
Question 5
A FOR i ← 0 TO 3
B IF Item[i] > Item[i] + 1 THEN
C CALL Swap(i)
D ENDIF
E NEXT i
Line B — The condition must compare neighbouring elements. Item[i] + 1 adds 1 to the value instead of looking at the next element. It should be IF Item[i] > Item[i + 1] THEN.

Student with a class code? Join your class →