What an algorithm is, through one way to sort: bubble sort compares neighbours and swaps them, pass after pass. Count the comparisons and swaps, and see how the starting row changes the work.
Step 1 of 6 · Exact steps
Step 1 of 6
Exact steps
An algorithm is a finite list of exact steps that solves a problem: each step is so clear it can be carried out without thinking, and the steps always end with the right answer. A recipe is nearly an algorithm; long division is one entirely. A computer can do nothing else: every program is an algorithm written in a language it understands.
The problem here is sorting: putting a row of blocks in order from shortest to tallest. Algorithm is set to Bubble, and its rule is short:
- Compare two neighbours.
- If the left one is taller, swap them.
- Move on to the next pair, to the end of the row. That is one pass.
- Repeat passes until a whole pass goes by without a single swap.
Watch the blocks: a blue bracket marks a comparison of a pair already in order, and an orange one marks a swap. The plaque under the shelf counts the comparisons and the swaps.
Step 2 of 6
One pass
To compare algorithms without a stopwatch, count their basic steps. In sorting, the basic step is a comparison of two blocks: how many comparisons the algorithm makes before the row is sorted.
Step 3 of 6
The tallest drifts to the end
The name “bubble sort” comes from what happens on every pass: the tallest block floats to the right, like a bubble rising through water. Watch the first pass and think about what it guarantees.
Step 4 of 6
Every pair
Starting order is set to Reversed: the blocks run from tallest to shortest, and every pass will have to swap. Each pass is one shorter than the last, because the block that floated to the end is already in place.
The sum is the number of pairs that can be made from blocks:
Step 5 of 6
Counting swaps
A comparison is only half the work; some comparisons end in a swap. Call two blocks with the taller one on the left an inverted pair. Swapping two neighbours fixes exactly their pair, and changes no other pair.
Step 6 of 6
Stopping early
Bubble sort does not keep working for nothing: after a whole pass without a single swap the row is sorted, and it stops. So how much it works depends not only on List size but also on the row it starts from.
Loading the interactive version…
Loading the interactive version…
Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.
Practice · Choose
What makes a list of instructions an algorithm?
Practice · Choose
What makes a list of instructions an algorithm?
Practice · Calculate
The row has 8 blocks. In bubble sort’s first pass each block is compared with its right-hand neighbour, left to right. How many comparisons does that pass make?
Practice · Calculate
The row has 8 blocks. In bubble sort’s first pass each block is compared with its right-hand neighbour, left to right. How many comparisons does that pass make?
Practice · Choose
Bubble sort has finished its first pass. What is certainly true now?
Practice · Choose
Bubble sort has finished its first pass. What is certainly true now?
Practice · Calculate
Algorithm is Bubble, List size 8, and Starting order Reversed: the tallest block first. How many comparisons will the whole sort make?
Practice · Calculate
Algorithm is Bubble, List size 8, and Starting order Reversed: the tallest block first. How many comparisons will the whole sort make?
Practice · Calculate
On the same reversed row of 8 blocks, how many swaps will bubble sort make?
Practice · Calculate
On the same reversed row of 8 blocks, how many swaps will bubble sort make?
Practice · Predict, then look
Starting order will change to Nearly sorted: 2 1 3 4 5 6 7 8, with only the first two blocks out of order. Bubble sort stops after a pass that swaps nothing. How many comparisons will it make?
Practice · Predict, then look
Starting order will change to Nearly sorted: 2 1 3 4 5 6 7 8, with only the first two blocks out of order. Bubble sort stops after a pass that swaps nothing. How many comparisons will it make?
Lesson complete. Every practice question here is done.
