ViewWiki

Lesson 1 of 4

The steps of an algorithm.

About 25 minutes · 6 practice questions

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:

  1. Compare two neighbours.
  2. If the left one is taller, swap them.
  3. Move on to the next pair, to the end of the row. That is one pass.
  4. 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.

To the question

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.

To the question

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.

To the question

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 1+2+⋯+(n−1)1 + 2 + \cdots + (n-1)⁠ is the number of pairs that can be made from nn⁠ blocks:

1+2+⋯+(n−1)=n(n−1)21 + 2 + \cdots + (n-1) = \frac{n(n-1)}{2}⁠

To the question

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.

To the question

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.

To the question

Still of the Algorithms and sorting 3D scene
Interactive scene

Loading the interactive version…

Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.

To the experiment

Practice · Choose

What makes a list of instructions an algorithm?

To the experiment

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?

To the experiment

Practice · Choose

Bubble sort has finished its first pass. What is certainly true now?

To the experiment

Practice · Calculate

Algorithm is Bubble, List size 8, and Starting order Reversed: the tallest block first. How many comparisons will the whole sort make?

To the experiment

Practice · Calculate

On the same reversed row of 8 blocks, how many swaps will bubble sort make?

To the experiment

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?