← IGCSE Computer Science Paper 2

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).

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
An array has n items. How many passes does the outer loop of a bubble sort need?
FOR Pass ← 1 TO n - 1FOR Pass ← 1 TO n + 1
Matches
FOR Pass ← 1 TO n FOR i ← 1 TO n - Pass IF Data[i] > Data[i + 1] THEN
↓
The outer loop runs to n instead of n - 1, so it does one pass too many
Applying
Meanings
Term
FOR Pass ← 1 TO n - 1
Meaning
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.
Practice
FOR Pass ← 1 TO n - 1
Testing
Gaps
FOR Pass ← 1 TO
Check Answer
Spots
1FOR Pass ← 1 TO n - 2
2 FOR i ← 1 TO n - Pass

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

The 'Parrotfish' badge — Function Builder, 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. An array has n items. How many passes does the outer loop of a bubble sort need?
A. FOR Pass ← 1 TO n + 1
B. FOR Pass ← 1 TO n - 1 ✓
n - 1 passes are enough. After n - 1 passes every item is in its place, so the last item is sorted automatically.
2. An array has 6 items. How many times does the outer loop run?
A. 5 times ✓
B. 6 times
The outer loop runs n - 1 times, so 6 - 1 = 5 passes.
3. Which inner loop header shrinks with each pass (array indexes start at 1)?
A. FOR Index ← 1 TO n - Pass ✓
B. FOR Index ← 1 TO n
Each pass puts one more large value at the end, so the inner loop can stop one place earlier each time: n - Pass.
4. Which line correctly compares two neighbours inside the inner loop?
A. IF Numbers[Index] > Numbers[Index] + 1 THEN
B. IF Numbers[Index] > Numbers[Index + 1] THEN ✓
Index + 1 goes inside the square brackets to pick the next element. Outside the brackets it would just add 1 to the value.
5. Why can the inner loop bound get smaller after each pass?
A. The largest unsorted value has bubbled to the end and is already in place ✓
B. The smallest value has moved to the start and the rest is sorted
In an ascending bubble sort, each pass carries the largest remaining value to the end of the unsorted part, so that position needs no further checks.

Match the Pairs

FOR Pass ← 1 TO n - 1 → The outer loop: it repeats the pass through the array n - 1 times
FOR i ← 1 TO n - Pass → The inner loop: it compares neighbours, and its bound shrinks after each pass
Inside the inner loop, compare two neighbours and swap them if they are out of order → IF Data[i] > Data[i + 1] THEN Temp ← Data[i] Data[i] ← Data[i + 1] Data[i + 1] ← Temp ENDIF
Close the blocks in the right order: ENDIF first, then the inner NEXT, then the outer NEXT → ENDIF NEXT i NEXT Pass
FOR Pass ← 1 TO n FOR i ← 1 TO n - Pass IF Data[i] > Data[i + 1] THEN → The outer loop runs to n instead of n - 1, so it does one pass too many
ENDIF NEXT Pass NEXT i → The NEXT statements are in the wrong order: the loops are not closed from the inside out

Key Term

FOR Pass ← 1 TO n - 1
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.

Fill in the Blanks

1. FOR Pass ← 1 TO n - 1 FOR i ← 1 TO n - Pass IF List[i] > List[i + 1] THEN
Hint: Type the upper limit of the outer loop: the number of passes, written using n, the number of items.
2. FOR Pass ← 1 TO n - 1 FOR i ← 1 TO n - Pass IF List[i] > List[i + 1] THEN
Hint: Type the outer loop variable that makes the inner loop bound shrink on each pass.
3. FOR i ← 1 TO n - Pass IF List[i] > List[i + 1] THEN Temp ← List[i]
Hint: Type the comparison operator that is true when two neighbours are in the wrong order for an ascending sort.
4. Temp ← List[i] List[i] ← List[i + 1] List[i + 1] ← Temp
Hint: Type the variable that holds the saved value so the swap does not lose it.
5. ENDIF NEXT i NEXT Pass
Hint: Type the keyword that closes the selection inside the inner loop, before the inner loop is closed.

Spot the Error

Question 1
A FOR Pass ← 1 TO n - 2
B FOR i ← 1 TO n - Pass
C CALL CompareSwap(A, i)
D NEXT i
E NEXT Pass
Line A — 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.
Question 2
A FOR Pass ← 1 TO n - 1
B FOR i ← 1 TO n - Pass
C CALL CompareSwap(A, Pass)
D NEXT i
E NEXT Pass
Line C — 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).
Question 3
A FOR Pass ← 1 TO n - 1
B FOR i ← 1 TO Pass
C CALL CompareSwap(A, i)
D NEXT i
E NEXT Pass
Line B — The inner loop bound should shrink as the passes go on, so it must be n - Pass. Using Pass makes it grow instead. It should be FOR i ← 1 TO n - Pass.
Question 4
A n ← 8
B FOR Pass ← 2 TO n - 1
C FOR i ← 1 TO n - Pass
D CALL CompareSwap(A, i)
E NEXT i
F NEXT Pass
Line B — The outer loop must start at 1 to give n - 1 passes. Starting at 2 gives only n - 2 passes, so the array may not be sorted. It should be FOR Pass ← 1 TO n - 1.
Question 5
A FOR Pass ← 1 TO n - 1
B FOR i ← 1 TO n - Pass
C CALL CompareSwap(A, i)
D NEXT Pass
E NEXT Pass
Line D — The inner loop must be closed with NEXT i before the outer loop is closed. The loops must be closed in the reverse order they were opened. It should be NEXT i.

Student with a class code? Join your class →