Worked example · Computer Science · Year 7

Unplugged algorithms: sorting networks

DCF: Data and computational thinking → Problem-solving and modelling

What this is

A completed Year 7 sheet. Every trace below is the real output of the network on the stimulus sheet — it has been checked against all 720 possible orderings of six different numbers, and it sorts every one of them. Show this after pupils have finished tracing; it is also your mark scheme.

Trace table — Card set B

After W1W2W3W4W5W6
Start124625335118
Step 1 — (1,6) (2,4) (3,5)123325465118
Step 2 — (2,3) (4,5)122533465118
Step 3 — (1,4) (3,6)122518465133
Step 4 — (1,2) (3,4) (5,6)122518463351
Step 5 — (2,3) (4,5)121825334651

Comparison by comparison: Step 1 — (1,6) 12 against 18, 12 is smaller and already above, nobody moves; (2,4) 46 against 33, swap; (3,5) 25 against 51, stay. Step 2 — (2,3) 33 against 25, swap; (4,5) 46 against 51, stay. Step 3 — (1,4) 12 against 46, stay; (3,6) 33 against 18, swap. Step 4 — (1,2) 12 against 25, stay; (3,4) 18 against 46, stay; (5,6) 51 against 33, swap. Step 5 — (2,3) 25 against 18, swap; (4,5) 46 against 33, swap.

Every one of the twelve comparisons is written down, including the seven where nothing moved — that is what makes the trace checkable, and it meets the first two success criteria.

The comparison that surprised us

Step 3 was the strange one. After step 2 the top four wires held 12, 25, 33, 46 and they were already in order, so we expected the network to leave them alone. Instead (3,6) pulled 18 up from the bottom into wire 3 and pushed 33 down, which made the top look worse for a moment. It only came right in step 5. So a step of an algorithm does not have to improve things to be useful — it has to move the right numbers into reach of the comparators that come next.

Follows the algorithm faithfully even where it looks wrong, rather than "fixing" it by sorting in their head.

Does it always work? The three tests

Test 1 — worst case, 51, 46, 33, 25, 18, 12. Out comes 12, 18, 25, 33, 46, 51. It was actually the fastest run of the three: step 1 swapped all three pairs (51/12, 46/25, 33/18) giving 12, 25, 18, 46, 33, 51, step 2 swapped both pairs giving 12, 18, 25, 33, 46, 51, and steps 3, 4 and 5 then had nothing left to do. The worst possible input for a person is not the worst input for this network.

Test 2 — already sorted, 12, 18, 25, 33, 46, 51. Out comes 12, 18, 25, 33, 46, 51 and there are zero swaps. All twelve comparisons still happen — the network cannot skip them, because no comparator knows what any other comparator found. A network always does the same amount of work whatever goes in.

Test 3 — repeats, 3, 3, 7, 1, 7, 1. Out comes 1, 1, 3, 3, 7, 7. The rule still works: when the two cards are equal, "the smaller goes up" means neither is smaller, so nobody moves. Either choice would give the same answer, so equal values are safe.

Three awkward inputs tested and the result explained, not just recorded — third success criterion.

Our three-wire network

Three-wire sorting network (1,2) (2,3) (1,2) IN 1 IN 2 IN 3 OUT OUT OUT Comparators in order: (1,2) then (2,3) then (1,2).

Our comparators, in order: (1,2)   (2,3)   (1,2).

Test with 3 on wire 1, 1 on wire 2, 2 on wire 3. (1,2): 3 against 1, swap → 1, 3, 2. (2,3): 3 against 2, swap → 1, 2, 3. (1,2): 1 against 2, stay → 1, 2, 3. Sorted.

We then tried all six possible orders of 1, 2 and 3 and every one came out 1, 2, 3.

A design of their own, tested rather than assumed, and tested against every possible input rather than one lucky one — fourth success criterion.

Counting questions

1. 12 comparators: 3 + 2 + 2 + 3 + 2.

2. 5 steps, because the comparators inside one step never share a wire, so all of them can happen at the same time.

3. 6 × 5 ÷ 2 = 15 comparisons to compare every pair. The network does only 12, so it already does three comparisons fewer. The bigger saving is the time: 12 comparisons but only 5 steps, because up to three happen at once. Comparing every pair one at a time would take 15 steps — three times as long.

4. Three comparators. Two is not enough: with (1,2) then (2,3), put in 2, 3, 1. The first comparator compares 2 and 3 and leaves them alone; the second compares 3 and 1 and swaps them, giving 2, 1, 3 — wrong. Whatever two comparators you choose, some pair never meets after the other numbers have moved, so an input exists that defeats it.

Distinguishes the number of comparisons from the number of steps, and backs up the "two is not enough" claim with a counterexample rather than an opinion — fifth success criterion.

Extension: Card set A, the Welsh mountains

After W1W2W3W4W5W6
Start10858938869051001915
Step 1 — (1,6) (2,4) (3,5)91589388690510011085
Step 2 — (2,3) (4,5)91588689390510011085
Step 3 — (1,4) (3,6)90588689391510011085
Step 4 — (1,2) (3,4) (5,6)88690589391510011085
Step 5 — (2,3) (4,5)88689390591510011085

Wire 1 ends up holding 886 m — Pen y Fan, the lowest of the six. Wire 6 holds 1085 m, Yr Wyddfa. Note that Yr Wyddfa reached wire 6 in step 1 and never moved again: the network puts the largest value in place first.

The extension is traced with the same discipline as the main task, and the pupil notices a property of the network rather than just reporting the answer.

Why this response is secure