# Sorting network

A simple sorting network consisting of four wires and five connectors

A sorting network is an abstract mathematical model of a network of wires and comparator modules that is used to sort a sequence of numbers. Each comparator connects two wires and sorts the values by outputting the smaller value to one wire, and the larger to the other. The main difference between sorting networks and comparison sorting algorithms is that with a sorting network the sequence of comparisons is set in advance, regardless of the outcome of previous comparisons. This independence of comparison sequences is useful for parallel execution of the algorithms. Despite the simplicity of the model, sorting network theory is surprisingly deep and complex.

## Introduction

Demonstration of a comparator in a sorting network.

A sorting network consists of two types of items: comparators and wires. The wires are thought of as running from left to right, carrying values (one per wire) that traverse the network all at the same time. Each comparator connects two wires. When a pair of values, traveling through a pair of wires, encounter a comparator, the comparator swaps the values if and only if the top wire's value is greater than the bottom wire's value.

In a formula, if the top wire carries x and the bottom wire carries y, then after hitting a comparator the wires carry $x' = \min(x, y)$ and $y' = \max(x, y)$, respectively, so the pair of values is sorted.[1]:635 A network of wires and comparators that will correctly sort all possible inputs into ascending order is called a sorting network.

The full operation of a simple sorting network is shown below. It is easy to see why this sorting network will correctly sort the inputs; note that the first four comparators will "sink" the largest value to the bottom and "float" the smallest value to the top. The final comparator simply sorts out the middle two wires.

### Depth and efficiency

The depth of a sorting network is defined (informally) as the largest number of comparators that any input value can encounter on its way through the network. Noting that sorting networks can perform certain comparisons in parallel (represented in the graphical notation by comparators that lie on the same vertical line), and assuming all comparisons to take unit time, it can be seen that the depth of the network is equal to the number of time steps required to execute it.[1]:636–637

### Insertion and selection networks

We can easily construct a network of any size recursively using the principles of insertion and selection. Assuming we have a sorting network of size n, we can construct a network of size n + 1 by "inserting" an additional number into the already sorted subnet (using the principle behind insertion sort). We can also accomplish the same thing by first "selecting" the lowest value from the inputs and then sort the remaining values recursively (using the principle behind bubble sort).

 A sorting network constructed recursively that first sinks the largest value to the bottom and then sorts the remaining wires. Based on bubble sort A sorting network constructed recursively that first sorts the first n wires, and then inserts the remaining value. Based on insertion sort

The structure of these two sorting networks are very similar. A construction of the two different variants, which collapses together comparators that can be performed simultaneously shows that, in fact, they are identical.

 Bubble sorting network Insertion sorting network When allowing for parallel comparators, bubble sort and insertion sort are identical

### Efficient networks

The insertion network has a large depth of O(n) making it impractical. There are simple networks which achieve depth O((log n)2) (hence size O(n (log n)2)), such as Batcher odd–even mergesort, bitonic sort, Shell sort, and the Pairwise sorting network. These networks are often used in practice.

### Zero-one principle

While it is easy to prove the validity of some sorting networks (like the insertion/bubble sorter), it is not always so easy. There are n! permutations of numbers in an n-wire network, and to test all of them would take a significant amount of time, especially when n is large. The number of test cases can be reduced significantly, to 2n, using the so-called zero-one principle. While still exponential, this is smaller than n! for all n >= 4, and the difference grows rapidly with increasing n.

The zero-one principle states that a sorting network is valid for arbitrary ordered inputs, if it can correctly sort all 2n sequences of 0s and 1s. This not only drastically cuts down on the number of tests needed to ascertain the validity of a network, it is of great use in creating many constructions of sorting networks as well.

The principle can be proven by first observing the following fact about comparators: when a monotonic function f is applied to the inputs, i.e., x and y are replaced by f(x) and f(y), then the comparator produces $\min(f(x), f(y)) = f(\min(x, y))$ and $\max(f(x), f(y)) = f(\max(x, y))$. By induction, this result can be extended to a lemma stating that if the network transforms the sequence a1, ... an into b1, ... bn, it will transform f(a1), ... f(an) into f(b1), ... f(bn). The proof of the zero-one principle proceeds by contradiction: support that some input a1, ... an contains two items ai < aj, and the network incorrectly swaps these in the output. Then it will also incorrectly sort f(a1), ... f(an) for the binary function f(x) = 1 if and only if x > ai, which is monotonic.[1]:640–641

The principle has been proven by a special case of Bouricius's theorem[2] in 1954 by W. G. Bouricius.[clarification needed]

### Complexity of testing sorting networks

It is unlikely that significant further improvements can be made for testing general sorting networks, unless P=NP, because the problem of testing whether a candidate network is a sorting network is known to be co-NP-complete.[3]

## Optimal sorting

The efficiency of a sorting network can be measured by its total size (the number of comparators used), or by its depth (the maximum number of comparators along any path from an input to an output). The asymptotically best known sorting network, called AKS network after its discoverers Ajtai, Komlós, and Szemerédi, achieves depth O(log n) and size O(n log n) for n inputs, which is asymptotically optimal. A simplified version of the AKS network was described by Paterson. While an important theoretical discovery, the AKS network has little or no practical application because of the large linear constants hidden by the Big-O notation. These are partly due to a construction of an expander graph. Finding sorting networks with size cn log n for small c remains a fundamental open problem.

Some important progress in designing optimal sorting network is done using genetic algorithm techniques as well. (M. Mitchell, 1998)

For 1 to 10 inputs optimal sorting networks are known. They have respectively 0, 1, 3, 5, 9, 12, 16, 19, 25 and 29[4] comparators.[2] The optimal depths for up to 16 inputs[5] are known and are respectively 0, 1, 3, 3, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9.

## References

1. ^ a b c Cormen, Thomas H.; Leiserson, Charles E., Rivest, Ronald L. (1990). Introduction to Algorithms (1st ed.). MIT Press and McGraw-Hill. ISBN 0-262-03141-8.
2. ^ a b Knuth, D. E. (1997). The Art of Computer Programming, Volume 3: Sorting and Searching (Third Edition ed.). Addison–Wesley. pp. 219–247. ISBN 0-201-89685-0. Section 5.3.4: Networks for Sorting.
3. ^ Parberry, Ian (1991). "On the Computational Complexity of Optimal Sorting Network Verification". Proc. PARLE '91: Parallel Architectures and Languages Europe, Volume I: Parallel Architectures and Algorithms, Eindhoven, The Netherlands: 252–269.
4. ^ Codish, Michael; Cruz-Filipe, Luís; Frank, Michael; Schneider-Kamp, Peter (2014). "Twenty-Five Comparators is Optimal when Sorting Nine Inputs (and Twenty-Nine for Ten)". arXiv:1405.5754 [cs.DM].
5. ^ Bundala, D.; Závodný, J. (2014). "Optimal Sorting Networks". Language and Automata Theory and Applications. Lecture Notes in Computer cience 8370. p. 236. arXiv:1310.6271. doi:10.1007/978-3-319-04921-2_19. ISBN 978-3-319-04920-5.