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.
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
bubble sort traceFill in the Blanks
Spot the Error
Student with a class code? Join your class →