Bubble sort that stops when no swaps are made

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Bubble sort that stops when no swaps are made

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Swapped ← FALSE be placed in a bubble sort that stops early?
A Once, after the whole sort has finished
B At the start of each pass, before the inner loop
2. When should Swapped ← TRUE be executed?
A Whenever two values are swapped
B Whenever two values are compared
3. Swapped is FALSE at the end of a full pass. What does this tell you?
A No swaps were needed, so the data is sorted
B The pass failed, so it must be repeated
4. Which loop ending is correct for a REPEAT version of this bubble sort?
A UNTIL Swapped = TRUE
B UNTIL Swapped = FALSE
5. Which WHILE condition keeps the bubble sort running correctly?
A WHILE Swapped = TRUE DO
B WHILE Swapped = FALSE DO

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Swapped ← FALSE AStop repeating the passes once a whole pass has made no swaps, so the data is sorted
2UNTIL Swapped = FALSE BSwapped ← TRUE
3When two neighbouring values are out of order and get swapped, the flag must record that a swap happened CWHILE Swapped = TRUE DO
4A WHILE version keeps looping as long as the last pass made a swap DReset the flag at the start of each pass, assuming no swaps have happened yet
5REPEAT FOR i ← 1 TO 4 ... NEXT i Swapped ← FALSE UNTIL Swapped = FALSE ESwapped is reset to FALSE after the pass instead of before it, so it erases any swap that was recorded and the loop always stops after one pass.
6IF Numbers[i] > Numbers[i + 1] THEN Temp ← Numbers[i] Numbers[i] ← Numbers[i + 1] Numbers[i + 1] ← Temp ENDIF FSwapped ← TRUE is missing inside the IF, so the flag stays FALSE even when a swap happens and the sort may stop too early.

Definitions

UNTIL Swapped = FALSE

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
REPEAT Swapped ← _____ FOR i ← 1 TO 9
2.
IF Numbers[i] > Numbers[i + 1] THEN Swapped ← ____ ENDIF
3.
NEXT i UNTIL Swapped = _____
4.
Swapped ← TRUE WHILE Swapped = ____ DO Swapped ← FALSE
5.
// Sorted data: no swaps in pass 1 UNTIL _______ = FALSE OUTPUT "Done"

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Swapped : INTEGER
B Swapped ← FALSE
C IF A[1] > A[2] THEN
D Swapped ← TRUE
E ENDIF
Question 2
A WHILE
B Swapped ← FALSE
C CALL OnePass(Swapped)
D UNTIL Swapped = FALSE
Question 3
A Swapped ← FALSE
B IF Price[1] > Price[2] THEN
C Swapped = TRUE
D ENDIF
Question 4
A REPEAT
B Swapped ← TRUE
C FOR i ← 1 TO 4
D CALL CompareSwap(i, Swapped)
E NEXT i
F UNTIL Swapped = FALSE
Question 5
A FOR i ← 1 TO 4
B IF A[i] > A[i + 1] THEN
C Swapped ← "TRUE"
D ENDIF
E NEXT i

Answer Key

Bubble sort that stops when no swaps are made
Section A: Quick Questions
Q1
B — At the start of each pass, before the inner loop
Q2
A — Whenever two values are swapped
Q3
A — No swaps were needed, so the data is sorted
Q4
B — UNTIL Swapped = FALSE
Q5
A — WHILE Swapped = TRUE DO
Section B: Match the Pairs
Q1
Swapped ← FALSE → Reset the flag at the start of each pass, assuming no swaps have happened yet
Q2
UNTIL Swapped = FALSE → Stop repeating the passes once a whole pass has made no swaps, so the data is sorted
Q3
When two neighbouring values are out of order and get swapped, the flag must record that a swap happened → Swapped ← TRUE
Q4
A WHILE version keeps looping as long as the last pass made a swap → WHILE Swapped = TRUE DO
Q5
REPEAT FOR i ← 1 TO 4 ... NEXT i Swapped ← FALSE UNTIL Swapped = FALSE → Swapped is reset to FALSE after the pass instead of before it, so it erases any swap that was recorded and the loop always stops after one pass.
Q6
IF Numbers[i] > Numbers[i + 1] THEN Temp ← Numbers[i] Numbers[i] ← Numbers[i + 1] Numbers[i + 1] ← Temp ENDIF → Swapped ← TRUE is missing inside the IF, so the flag stays FALSE even when a swap happens and the sort may stop too early.
Section C: Definitions
Q1
UNTIL Swapped = FALSE — A BOOLEAN flag set to FALSE before each pass and to TRUE whenever a swap happens. The bubble sort stops when a full pass leaves it FALSE, meaning no swaps were needed and the data is sorted.
Section D: Fill in the Blanks
Q1
REPEAT Swapped ← FALSE FOR i ← 1 TO 9
Q2
IF Numbers[i] > Numbers[i + 1] THEN Swapped ← TRUE ENDIF
Q3
NEXT i UNTIL Swapped = FALSE
Q4
Swapped ← TRUE WHILE Swapped = TRUE DO Swapped ← FALSE
Q5
// Sorted data: no swaps in pass 1 UNTIL Swapped = FALSE OUTPUT "Done"
Section E: Spot the Error
Q1
A — Swapped is a flag that is only ever TRUE or FALSE, so it must be declared as BOOLEAN, not INTEGER.
Q2
A — The loop ends with UNTIL, so it must open with REPEAT. A WHILE loop would need DO and ENDWHILE.
Q3
C — = is a comparison, not an assignment. To record that a swap happened, use the assignment arrow: Swapped ← TRUE.
Q4
B — Swapped must be set to FALSE at the start of each pass. If it starts as TRUE, a pass with no swaps still ends TRUE and the loop never stops.
Q5
C — Swapped is a BOOLEAN, so it needs the value TRUE without quote marks. "TRUE" is a string, not a BOOLEAN.