πŸ“š Knowledge Library β€” Topic 7.10 β€” Algorithm Design & Problem Solving

Bubble Sort Explained Simply

Learn how bubble sort compares neighbouring values, swaps those in the wrong order and repeats passes until the list is sorted.

1. Invitation

Bubble sort puts data in order by comparing neighbours.

A bubble sort repeatedly compares neighbouring values.

If a pair is in the wrong order, the values are swapped.

πŸ’‘ Key idea: compare two neighbours, swap if needed, then move one place forward.
Figure 1.1
Compare and Swap
Compare pair
↓
Wrong order?
↓
Swap
↓
Move forward
2. Big Idea

One pass moves a large value towards the end.

During one pass, the algorithm moves from the start of the list to the end, comparing each neighbouring pair.

In ascending order, larger values gradually β€œbubble” towards the right.

πŸ’‘ A pass is one complete journey through the unsorted part of the list.
Figure 2.1
Bubble Towards the End
Large value
β†’
Moves right
β†’
Settles near end
3. FutureLogic Bridge

Think of students lining up by height.

Two neighbouring students compare heights. If the taller student is standing before the shorter student, they swap places.

The comparison then moves to the next pair. After several passes, the whole line is ordered.

πŸ’‘ Bridge: compare neighbours, swap places, repeat until the line is correct.
Figure 3.1
The Height Line
Compare pair
↓
Swap if taller first
↓
Move to next pair
4. The Four-Step Method

Compare, swap, move and repeat.

1Compare two neighbouring values.
2Swap them if they are in the wrong order.
3Move one position forward.
4Repeat passes until sorted.
πŸ’‘ The comparison changes depending on whether ascending or descending order is required.
5. Visual Walkthrough

Sort 6, 2, 5, 1 into ascending order.

Compare 6 and 2

6
2
5
1
↓ swap
2
6
5
1

Compare 6 and 5, then 6 and 1

2
5
1
6

The largest value is now in its final position.

Complete the remaining passes

2
1
5
6
↓ final swap
1
2
5
6
πŸ’‘ Each pass places at least one value into its correct final position.
6. When Does It Stop?

There are two common stopping rules.

The sort may stop after a maximum of number of items βˆ’ 1 passes.

A more efficient version stops early when a complete pass makes no swaps.

🎯 Exam Tip: no swaps in a full pass means the list is already sorted.
Figure 6.1
Stopping Rules
n βˆ’ 1 passes
or
No swaps in a pass
7. Ascending and Descending

The comparison depends on the required order.

For ascending order, swap when the left value is greater than the right value.

For descending order, swap when the left value is less than the right value.

πŸ’‘ Ascending: smaller first. Descending: larger first.
Figure 7.1
Choose the Comparison
Ascending
Left > Right β†’ swap

Descending
Left < Right β†’ swap
8. Computational Thinking

Bubble sort is a repeated pattern.

SkillHow it appears
DecompositionThe sort is divided into passes and comparisons.
Pattern recognitionThe same compare-and-swap pattern repeats.
AbstractionOnly neighbouring values and their order matter.
Algorithmic thinkingThe comparisons follow a precise sequence and stopping rule.
πŸ’‘ Repeated local decisions create a correctly ordered list.
Figure 8.1
Repeated Pattern
Compare
↓
Swap if needed
↓
Move forward
β†Ί
9. Worked Example

Pseudocode for an ascending bubble sort.

AlgorithmBubble sort
FOR Pass ← 1 TO LENGTH(Data) - 1
  FOR Index ← 0 TO LENGTH(Data) - 2
    IF Data[Index] > Data[Index + 1]
      THEN
        Temp ← Data[Index]
        Data[Index] ← Data[Index + 1]
        Data[Index + 1] ← Temp
    ENDIF
  NEXT Index
NEXT Pass

How it works

The outer loop controls the passes. The inner loop compares neighbouring values. Temp safely stores one value while the swap takes place.

Model answer: β€œThe algorithm repeatedly compares adjacent array elements and swaps them when the left value is greater than the right value.”
10. Why Use Temp?

A temporary variable prevents a value from being lost.

If the first value is overwritten immediately, it disappears before it can be moved.

Temp holds one value safely during the exchange.

πŸ’‘ Swap pattern: save first β†’ move second β†’ restore saved value.
Figure 10.1
Safe Swap
Temp ← A
↓
A ← B
↓
B ← Temp
11. Exam Tip

Show the list after each complete pass.

Keep comparisons in order and clearly separate one pass from the next.

🎯 Do not sort the list mentally and jump straight to the final answer. Show every required comparison or pass.
Figure 11.1
Show the Journey
Start list
↓
Pass 1
↓
Pass 2
↓
Sorted list
12. Common Mistake

Do not skip pairs or stop after one pass.

Compare neighbouring values only, and move exactly one position forward each time.

⚠️ One pass rarely sorts the whole list. Repeat until the stopping rule is met.
Figure 12.1
Reliable Method
Underline pair
↓
Compare
↓
Swap if needed
↓
Move one place
13. Summary

Bubble sort in one screen.

Bubble sort repeatedly compares neighbouring values and swaps those in the wrong order.

One complete journey through the data is called a pass. Passes continue until the maximum number is reached or a full pass makes no swaps.

A temporary variable is used when swapping values.

πŸ’‘ Final thought: compare neighbours, swap if needed, repeat until ordered.
Figure 13.1
Final Model
Compare pair
↓
Swap
↓
Complete pass
↓
Repeat or stop