Bubble sort with nested FOR loops
Outer loop of a bubble sort: it runs n - 1 passes. Inside it, an inner FOR loop compares neighbours and swaps them if out of order, so the whole array ends up sorted.
Understanding Bubble sort with nested FOR loops in IGCSE Computer Science Paper 2
What it does: The IF compares neighbouring elements Data[i] and Data[i + 1]. If they are in the wrong order, a temporary variable is used to swap them. The innermost block must be closed first: the IF is closed by ENDIF, then the inner loop by NEXT i, and last the outer loop by NEXT Pass. n - 1 passes are enough. After n - 1 passes every item is in its place, so the last item is sorted automatically. The outer loop runs n - 1 times, so 6 - 1 = 5 passes. Index + 1 goes inside the square brackets to pick the next element. Outside the brackets it would just add 1 to the value.
Common mistakes: The outer loop must run n - 1 passes. With n - 2 it stops one pass early, so the array may not be fully sorted. It should be FOR Pass ← 1 TO n - 1. The inner loop must compare the neighbours at the inner loop counter i. Using Pass compares the same position every time. It should be CALL CompareSwap(A, i).
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
FOR Pass ← 1 TO n - 1Fill in the Blanks
Spot the Error
Student with a class code? Join your class →