TAOCP 5.3.4 Exercise 16

We prove that the algorithm transforms any sorting network into a standard sorting network of the same length.

Section 5.3.4: Networks for Sorting

Exercise 16. [M22] Prove that the following algorithm transforms any sorting network $[i_1:j_1]\ldots[i_r:j_r]$ into a standard sorting network of the same length:

T1. Let $q$ be the smallest index such that $i_q > j_q$. If no such index exists, stop.

T2. Change all occurrences of $i_q$ to $j_q$, and all occurrences of $j_q$ to $i_q$, in all comparators $[i_s:j_s]$ for $q \le s \le r$. Return to T1. ▮

Thus, $[4:1][3:2][1:3][2:4][1:2][3:4]$ is first transformed into $[1:4][3:2][1:3][2:1][4:2][3:1]$, then $[1:4][2:3][1:4][2:3][1:3][1:2][1:3][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:2][1:3][1:2][3:4][1:4]$, etc., until the standard network $[1:4][2:3][2:4][1:3][1:2][3:4]$ is obtained.

Verified: no
Solve time: 17m26s


Solution

We prove that the algorithm transforms any sorting network into a standard sorting network of the same length.
Let the network be $\alpha = [i_1:j_1][i_2:j_2]\ldots[i_r:j_r]$.
A comparator $[i:j]$ is standard if $i<j$; the network is standard if all its comparators are standard.

1. Termination

At each iteration the algorithm finds the smallest index $q$ with $i_q > j_q$.
All comparators before $q$ are unchanged and remain standard.
After the transformation the $q$-th comparator becomes $[j_q:i_q]$ with $j_q < i_q$, so it is standard.
Consequently the new first nonstandard comparator (if any) has index $> q$.
Since $q$ strictly increases and is bounded by $r$, after at most $r$ steps no such $q$ exists and the algorithm stops.
The final network is therefore standard and has the same length $r$.

2. Effect of one transformation step

Suppose at some step the current network is $\alpha$ and the chosen index is $q$ with $i_q > j_q$.
Let $\pi$ be the transposition that swaps the two lines $i_q$ and $j_q$.
The transformation T2 replaces every comparator $[i_s:j_s]$ for $s \ge q$ by $[\pi(i_s):\pi(j_s)]$; the first $q-1$ comparators stay the same.
Denote the resulting network by $\alpha'$.

We claim that for every input $x$, [ \alpha'(x) = \pi(\alpha(x)). ]

Proof of the claim.
Let $A$ be the operation of the first $q-1$ comparators, let $C = [i_q:j_q]$, and let $B$ be the operation of the remaining comparators.
Then $\alpha = B \circ C \circ A$.
The new network is $\alpha' = B' \circ C' \circ A$ where $C' = [j_q:i_q] = \pi C \pi$ and $B'$ is obtained from $B$ by applying $\pi$ to all wire indices, i.e. $B' = \pi B \pi$.
Thus [ \alpha' = (\pi B \pi) \circ (\pi C \pi) \circ A = \pi B C \pi A. ] Now observe that the comparator $C = [i_q:j_q]$ satisfies $C(y) = C(\pi(y))$ for every state $y$:
indeed $C$ puts the minimum of the two values on line $i_q$ and the maximum on line $j_q$;
$\pi(y)$ simply swaps the two values before $C$ acts, so the minimum and maximum end up on the same two lines (only their labels are interchanged, but $C$ itself does the same interchange).
Hence $C \circ A(x) = C(\pi(A(x)))$ for all $x$, and therefore $B C A(x) = B C \pi A(x)$.
Applying $\pi$ to both sides gives [ \pi(B C A(x)) = \pi(B C \pi A(x)) = \alpha'(x), ] which is exactly $\alpha'(x) = \pi(\alpha(x))$. ∎

3. Composition of the steps

If the algorithm performs $m$ steps with transpositions $\pi_1,\pi_2,\ldots,\pi_m$ (in that order), then by induction on $k$ the network $\alpha_k$ obtained after $k$ steps satisfies [ \alpha_k(x) = \pi_k \cdots \pi_1(\alpha_0(x)) \qquad\text{for all } x. ] In particular, the final standard network $\alpha^$ satisfies [ \alpha^(x) = \pi_{\text{total}}(\alpha(x)) \quad\text{where}\quad \pi_{\text{total}} = \pi_m \cdots \pi_1. ]

4. $\pi_{\text{total}}$ must be the identity

Because $\alpha$ is a sorting network, $\alpha(x)$ is the nondecreasing sequence of the bits of $x$ (the sorted version of $x$).
Thus $\alpha^*(x) = \pi_{\text{total}}(\text{sorted}(x))$.

We now show that $\pi_{\text{total}}$ is forced to be the identity permutation.
Consider the $0$-$1$ sequences with exactly $k$ ones. Their sorted versions are the sequences [ y^{(k)} = (\underbrace{0,\ldots,0}{n-k},\underbrace{1,\ldots,1}{k}) . ] Since $\alpha$ is a sorting network, every such $y^{(k)}$ is in the image of $\alpha$.
The final network $\alpha^*$ is standard; in particular it contains no comparator $[i:j]$ with $i>j$.
A standard network that is not a sorting network would fail to sort some $y^{(k)}$ after applying $\pi_{\text{total}}$.
We prove by induction on $k$ that $\pi_{\text{total}}$ fixes the last $k$ positions for all $k$, hence is the identity.

Base $k=1$. The sequence $y^{(1)}$ has a single $1$ at position $n$.
$\pi_{\text{total}}(y^{(1)})$ has a single $1$ at position $\pi_{\text{total}}(n)$.
For $\alpha^*$ to be a sorting network, $\pi_{\text{total}}(y^{(1)})$ must be sorted, which forces $\pi_{\text{total}}(n)=n$.

Inductive step. Assume $\pi_{\text{total}}$ fixes positions $n,n-1,\ldots,n-k+2$.
Consider $y^{(k)}$ whose ones occupy the last $k$ positions.
$\pi_{\text{total}}(y^{(k)})$ has ones at positions $\pi_{\text{total}}(n-k+1),\pi_{\text{total}}(n-k+2),\ldots,\pi_{\text{total}}(n)$.
By the induction hypothesis the last $k-1$ of these are $n-k+2,\ldots,n$.
For the result to be sorted (i.e. a block of ones at the end), the remaining one must be at $n-k+1$.
Hence $\pi_{\text{total}}(n-k+1)=n-k+1$.

By induction $\pi_{\text{total}}(i)=i$ for all $i$; thus $\pi_{\text{total}}$ is the identity permutation.

5. Conclusion

Since $\pi_{\text{total}} = \text{id}$, we have $\alpha^(x) = \alpha(x)$ for all $x$.
Because $\alpha$ is a sorting network, $\alpha^
$ is also a sorting network.
It is standard by construction and has the same length as $\alpha$. ∎

Notes

The verification that $\pi_{\text{total}}=\text{id}$ uses the zero‑one principle and the fact that the set of sorted $0$‑$1$ sequences is exactly ${0^a1^b}$. The argument that the total permutation is forced to be the identity is independent of the particular sequence of transpositions; it relies only on the original network being a sorting network and the final network being standard. The proof also shows that the algorithm effectively “undoes” all wire swaps that would otherwise disturb the sorted order, yielding a standard network that computes exactly the same function as the original.