A sorting network is an arrangement of 2 input comparators, which can sort an input sequence of n elements. For example, here's a sorting network for 9 element input:
Each of the vertical lines is a 2 input comparator, the input sequence enters on the left, and the sorted sequence appears on the right.
My question is: how to prove that if we remove the top or bottom line of any valid n input sorting network, we will end up with a valid sorting network for (n-1) inputs? What about removing any of the middle lines?
I have a feeling that this could be shown using a graph representation of a sorting network, but I can't find a suitable representation.
The top or bottom line can indeed be removed. One way to prove this is using Knuth's 0-1 principle, which states that a sorting network is correct if and only if every sequence of zeros and ones is sorted correctly. Let S
be a sorting network and let S'
be S
with the top line removed. Let x
be a 0-1 input to S'
. Pass 0x
to S
. By induction, we can show that the values after k
stages agree (except for the removed top line), since all of the gates involving the top line are no-ops. It follows that S'
is a correct sorting network.
In general, we cannot remove a middle line. Consider, for example, the network
1 * *
| |
2 * * *
|
3 *