← IGCSE Computer Science Paper 2

Tracing a bubble sort

Writing out the array after every pass, showing each comparison and swap, plus the Temp and Swapped values, to see how a bubble sort puts the data in order.

Understanding Tracing a bubble sort in IGCSE Computer Science Paper 2

What it does: Each time two items are swapped, the algorithm sets Swapped to TRUE. The trace shows this as a change in the Swapped column. An inner loop that runs 1 TO 4 compares pairs (1,2), (2,3), (3,4) and (4,5). That is 4 comparisons for 5 items. Pass 1 compares 5,3 (swap), 5,8 (no swap), 8,1 (swap). One pass only moves the largest value to the end. It does not fully sort the array. 4,2 swap; 4,7 no swap; 7,1 swap; 7,3 swap. That is 3 swaps and gives [2, 4, 1, 3, 7]. Temp stores a copy of Data[j] first, so 6 goes into Temp. Then Data[j + 1] is copied into Data[j], and Temp is copied into Data[j + 1].

Common mistakes: Temp must store the first value, A[1], before it is overwritten. Storing A[2] in Temp means A[1] is lost and both elements end up equal. It should be Temp ← A[1]. A[J] has already been overwritten on line 2, so copying it would duplicate the new value. The original A[J] was saved in Temp, so the line should be A[J + 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
A bubble sort (ascending) is run on [5, 3, 8, 1]. What does the array look like after pass 1?
[1, 3, 5, 8][3, 5, 1, 8]
Matches
5 3 8 1 after pass 1: 1 3 5 8
↓
A single pass only swaps neighbouring items, so it cannot fully sort the array. After pass 1 the array is 3 5 1 8.
Applying
Meanings
Term
bubble sort trace
Meaning
Writing out the array after every pass, showing each comparison and swap, plus the Temp and Swapped values, to see how a bubble sort puts the data in order.
Practice
Swapped ← FALSE
Testing
Gaps
IF Data[i] Data[i + 1] THEN
Check Answer
Spots
1Temp ← A[2]
2A[1] ← A[2]

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

The 'Cheetah' badge — Savannah Sprinter, 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. A bubble sort (ascending) is run on [5, 3, 8, 1]. What does the array look like after pass 1?
A. [3, 5, 1, 8] ✓
B. [1, 3, 5, 8]
Pass 1 compares 5,3 (swap), 5,8 (no swap), 8,1 (swap). One pass only moves the largest value to the end. It does not fully sort the array.
2. Trace [4, 2, 7, 1, 3] through pass 1 of a bubble sort. How many swaps are made?
A. 2 swaps
B. 3 swaps ✓
4,2 swap; 4,7 no swap; 7,1 swap; 7,3 swap. That is 3 swaps and gives [2, 4, 1, 3, 7].
3. In the trace of [4, 4, 2], two equal values 4 and 4 are compared. What happens?
A. No swap, because 4 > 4 is false ✓
B. They are swapped, because they are the same
A swap only happens when the left value is greater than the right value. Equal values are left alone.
4. In a trace table, Data[j] is 6 and Data[j + 1] is 2. What value is written in the Temp column?
A. 2
B. 6 ✓
Temp stores a copy of Data[j] first, so 6 goes into Temp. Then Data[j + 1] is copied into Data[j], and Temp is copied into Data[j + 1].
5. A pass finishes and the Swapped column still shows FALSE. What does this tell you?
A. No swaps were made, so the array is sorted and the loop stops ✓
B. The array is not sorted, so another pass is needed
Swapped is set to TRUE only when a swap happens. If it is still FALSE at the end of a pass, the data is already in order.

Match the Pairs

5 3 8 1 → 3 5 1 8 → The array before and after pass 1 of a bubble sort on 5, 3, 8, 1. Adjacent pairs were compared, and the 8 reached the end.
Temp ← Numbers[i] → The Temp column holds the value copied out of the array just before a swap, so it is not lost when overwritten.
In a trace table, the Swapped column changes to TRUE on any comparison that causes a swap during a pass. → Swapped ← TRUE
With 5 items, one pass makes 4 comparisons, so the trace has 4 comparison rows for each pass. → FOR Index ← 1 TO 4
5 3 8 1 after pass 1: 1 3 5 8 → A single pass only swaps neighbouring items, so it cannot fully sort the array. After pass 1 the array is 3 5 1 8.
IF Numbers[i] >= Numbers[i + 1] THEN → Using >= swaps equal values for no reason. With duplicates, Swapped is set to TRUE even though nothing changes.

Key Term

bubble sort trace
Writing out the array after every pass, showing each comparison and swap, plus the Temp and Swapped values, to see how a bubble sort puts the data in order.

Fill in the Blanks

1. IF Data[i] > Data[i + 1] THEN // swap, equal values are left alone ENDIF
Hint: Type the comparison operator that swaps only when the left value is larger, so duplicates stay put.
2. Temp ← Data[i] Data[i] ← Data[i + 1] Data[i + 1] ← Temp
Hint: Type the variable that holds the first value during the swap, the one shown in the Temp column.
3. IF Data[i] > Data[i + 1] THEN Swapped ← TRUE ENDIF
Hint: Type the Boolean value that records in the Swapped column that a swap has happened.
4. // Data has 5 items FOR i ← 1 TO 4 IF Data[i] > Data[i + 1] THEN
Hint: Type the number of comparisons made in one pass when there are 5 items.
5. REPEAT Swapped ← FALSE FOR i ← 1 TO 4
Hint: Type the Boolean value Swapped is reset to at the start of every pass.

Spot the Error

Question 1
A Temp ← A[2]
B A[1] ← A[2]
C A[2] ← Temp
Line A — Temp must store the first value, A[1], before it is overwritten. Storing A[2] in Temp means A[1] is lost and both elements end up equal. It should be Temp ← A[1].
Question 2
A Temp ← A[J]
B A[J] ← A[J + 1]
C A[J + 1] ← A[J]
Line C — A[J] has already been overwritten on line 2, so copying it would duplicate the new value. The original A[J] was saved in Temp, so the line should be A[J + 1] ← Temp.
Question 3
A Start: 4 3 1
B Compare 4,3: swap -> 3 4 1
C Compare 4,1: no swap -> 3 4 1
D Pass 1 done: 2 swaps
Line C — 4 is greater than 1, so the pair must be swapped, giving 3 1 4. The trace should read: Compare 4,1: swap -> 3 1 4.
Question 4
A Start: 3 3 1
B Compare 3,3: swap -> 3 3 1
C Compare 3,1: swap -> 3 1 3
D Pass 1 done: 1 swap
Line B — Bubble sort only swaps when the left value is greater than the right value. Duplicate values are equal, so there is no swap. The line should say: no swap.
Question 5
A Swapped ← FALSE
B IF A[J] > A[J + 1] THEN
C Swapped ← FALSE
D ENDIF
Line C — When a swap happens inside the IF, the Swapped flag must be set to TRUE so the sort knows another pass is needed. It should be Swapped ← TRUE.

Student with a class code? Join your class →