The missing third operation must compare lanes 1 and 2.
The completed service manual is:
1. Compare lanes 1 and 3
2. Compare lanes 2 and 4
3. Compare lanes 1 and 2
4. Compare lanes 3 and 4
5. Compare lanes 2 and 3
Why must step 3 compare lanes 1 and 2?
There is a rather nice way to see this without trying all 24 possible starting orders.
After step 1, the lighter of the parcels that started on lanes 1 and 3 is on lane 1.
After step 2, the lighter of the parcels that started on lanes 2 and 4 is on lane 2.
So after the first two comparisons, the lightest parcel of all four must be sitting on either lane 1 or lane 2.
Now look at what happens later.
- Step 4 compares lanes 3 and 4.
- Step 5 compares lanes 2 and 3.
- Lane 1 is never touched again.
That means step 3 is our last chance to make certain that the lightest parcel reaches lane 1.
So step 3 has to compare lanes 1 and 2.
But does that really sort every possible input?
Yes โ and we can prove it without checking the possible starting arrangements one by one.
After the first two comparisons, lanes 1 and 2 contain the lighter parcel from each of the two original pairs, while lanes 3 and 4 contain the heavier parcel from each pair.
Step 3 compares the two lighter parcels. The lighter of those must be the lightest parcel of all four, so it is placed permanently on lane 1.
Step 4 compares the two heavier parcels. The heavier of those must be the heaviest parcel of all four, so it is placed permanently on lane 4.
That leaves just the two middle parcels on lanes 2 and 3. Step 5 compares them and puts them into the correct order.
So the machine is guaranteed to finish with the parcels ordered from lightest to heaviest on lanes 1 to 4.
A quick example
Suppose the parcels arrive in the order 4, 1, 3, 2, where 1 is the lightest and 4 the heaviest.
| Stage | Lane 1 | Lane 2 | Lane 3 | Lane 4 |
|---|---|---|---|---|
| Start | 4 | 1 | 3 | 2 |
| After 1 โ 3 | 3 | 1 | 4 | 2 |
| After 2 โ 4 | 3 | 1 | 4 | 2 |
| After 1 โ 2 | 1 | 3 | 4 | 2 |
| After 3 โ 4 | 1 | 3 | 2 | 4 |
| After 2 โ 3 | 1 | 2 | 3 | 4 |
Where this leads
What we have built is a tiny example of a sorting network.
The machine does not need to decide which comparison to perform next by looking at the parcels. The same five comparisons are carried out in the same order whatever weights arrive on the belts.
There are 24 possible starting orders for four different parcels, but this five-comparator network sorts every one of them.
Five comparisons is also the best we could possibly hope for. Four comparisons have only 24 = 16 possible patterns of outcomes, but there are 24 possible starting orders to distinguish.
So our slightly coffee-stained machine is not just working again. It is doing the job with the minimum possible number of comparisons.
