On this page
Quicksort and merge sort decide what to compare next by looking at the data. A sorting network fixes every comparison in advance, which makes it a natural fit for hardware and lets you test it on a short, fixed list of inputs.
What is a sorting network?
Picture n horizontal wires, each carrying one number from left to right. A comparator joins two wires and swaps their values if the top one is larger, so the smaller value always leaves on the top wire. A comparator network that sorts every possible input is called a sorting network, as defined in the sorting network article on Wikipedia.
Two numbers describe a network. Its size is the number of comparators. Its depth is the number of layers, where comparators in one layer touch different wires and can run at the same time.
How does a 4-input network with 5 comparators work?
Label the wires a, b, c, d from top to bottom. The network has three layers:
Compare (a, b) and (c, d) together.
Compare (a, c) and (b, d) together.
Compare (b, c).
Trace the input 3, 1, 4, 2:
Layer 1: (a, b) turns 3, 1 into 1, 3; (c, d) turns 4, 2 into 2, 4. Wires now read 1, 3, 2, 4.
Layer 2: (a, c) sees 1 and 2, no swap; (b, d) sees 3 and 4, no swap. Still 1, 3, 2, 4.
Layer 3: (b, c) sees 3 and 2 and swaps. Output: 1, 2, 3, 4.
Why it always works: after layer 2, wire a holds the smallest of all four values (the smaller of the two pair-minimums) and wire d holds the largest. Only the middle two can still be out of order, and the last comparator fixes them. The Wikipedia table of optimal networks lists 5 comparators and depth 3 for four inputs, and says size-optimal networks are known for one to twelve inputs.
What is the 0–1 principle, and why is it true?
The 0–1 principle says that if a comparator network sorts all 2^n sequences of zeros and ones, it sorts every input of n numbers. The Wikipedia article states it in these terms and cites the proof in Cormen, Leiserson and Rivest's Introduction to Algorithms.
The proof fits in three steps:
Take any rising step function f, for example f(x) = 1 if x is at least t, and 0 otherwise. A comparator outputs min and max, and f(min(x, y)) = min(f(x), f(y)), with the same for max, so applying f before or after the network gives the same result.
Suppose the network fails on some input: somewhere in the output a larger value p sits above a smaller value q.
Choose t = p. The zero-one input f(input) then comes out with a 1 above a 0, so the network also fails on a zero-one input.
So no zero-one failure means no failure at all. For four wires that is 16 checks instead of 24 orderings; for ten wires, 1,024 instead of 3,628,800.
The test catches mistakes. Remove the last comparator and feed 1, 0, 0, 1. Layer 1 gives 0, 1, 0, 1, layer 2 changes nothing, and the output 0, 1, 0, 1 is unsorted.
How do bubble-sort and Batcher networks scale?
Bubble sort becomes a network if you write out every compare it would make. For four wires that is (a, b), (b, c), (c, d), then (a, b), (b, c), then (a, b): 6 comparators, one more than the optimum. In general the count is 1 + 2 + ... + (n − 1) = n(n − 1)/2, and with overlapping passes the depth is 2n − 3.
Kenneth Batcher did much better in his 1968 paper Sorting networks and their applications. His odd–even merge sort and bitonic sort build networks of depth O(log² n), far fewer layers than bubble sort's 2n − 3 once n is large.
Why do sorting networks suit hardware and GPUs?
Because the comparisons never depend on the data, every input follows the same path. In a circuit, each comparator is a fixed part and a whole layer works at once.
In GPU Gems 2, chapter 46, Peter Kipfer and Rüdiger Westermann explain that data-independent sorts always take the same processing path, so the points where data must be exchanged are known in advance. That matters on a GPU whose fragment processors cannot choose where to write based on the data. Their implementation is built on Batcher's bitonic merge sort.
If the step-function argument behind the 0–1 proof felt shaky, the Fermi Concept Library covers every concept in mathematics and computing, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning.
For more counting with permutations, see how stack sorting links to 231-avoiding permutations, why every long sequence has a long monotone subsequence, and how the Gale–Shapley algorithm finds a stable matching.



