Join the waiting list

IGCSE Computer Science · Lesson · Unit 7: Algorithm Design and Problem-Solving

How do you trace a bubble sort?

Compare the first two items and swap them if they are in the wrong order, then move one place along and repeat to the end of the list. That is one pass, and it carries the largest value to the end. Keep making passes until one makes no swaps: then the list is sorted.

The lesson: Bubble Sort

Bubble sort

In this section

  • Understand how bubble sort works.
  • Trace passes through a list.
  • Write a bubble-sort algorithm.
  • Explain how adjacent values are compared and swapped.

A bubble sort puts a list in order by comparing each pair of adjacent items and swapping them if they are the wrong way round, repeating until a whole pass makes no swaps at all.

Follow 5, 3, 8, 1 through one pass. Compare 5 and 3: wrong order, swap, giving 3, 5, 8, 1. Compare 5 and 8: correct, leave them. Compare 8 and 1: swap, giving 3, 5, 1, 8. The pass is over and 8, the largest value, has reached the end — which is what happens on every pass, and is why each pass can stop one place earlier than the last.

The second pass gives 3, 1, 5, 8 and the third 1, 3, 5, 8. A fourth pass makes no swaps, which is how the algorithm knows to stop.

The structure is two nested loops: an outer loop over the passes, an inner loop over the adjacent pairs within a pass, and a swap inside that. The swap needs a temporary variable — temp ← a, a ← b, b ← temp — because assigning one to the other directly destroys the value you still need.

A flag set to False at the start of each pass and to True on any swap is what makes the algorithm stop early: if a pass completes with the flag still False, the list is already sorted and no further pass can change it. Bubble sort is simple to write and easy to trace, and it is slow on a large list, because the number of comparisons grows with the square of the number of items.

Key points

  • Compare adjacent pairs and swap if out of order; repeat until a pass makes no swaps.
  • Each pass carries the largest remaining value to the end.
  • A swap needs a temporary variable, or one of the two values is lost.

Now try it

Bubble sort, step by step

Bubble sort, step by stepComputer Science · Algorithms

Pass 1: will 5 and 2 swap?

comparing528163123456
Pass
1
Comparisons · swaps
0 · 0
Predictions right
0 of 0
REPEAT    Swapped ← FALSE    FOR Index ← 1 TO Last − 1        IF List[Index] > List[Index + 1]            THEN                Temp ← List[Index]                List[Index] ← List[Index + 1]                List[Index + 1] ← Temp                Swapped ← TRUE        ENDIF    NEXT Index    Last ← Last − 1UNTIL NOT Swapped

Tasks0 of 3 done

  • Finish the first pass, so the biggest number reaches the end.
  • Predict five comparisons correctly.
  • Sort the whole list.
Open Bubble sort, step by step on its own page

Common mistakes

  • Thinking one pass sorts the list, rather than putting one more value in its final place.
  • Not seeing why it stops: a pass with no swaps means the list is in order.