# A Sorting Network

**A sorting network**

We now have all the necessary tools to construct a network that can sort any input sequence. The sorting network SORTER[*n*] uses the merging network to implement a parallel version of merge sort from divide-and-conquer approach. The construction and operation of the sorting network are illustrated in Figure 27.12.

*n*] constructed by recursively combining merging networks.

*(a)*The recursive construction.

*(b)*Unrolling the recursion.

*(c)*Replacing the MERGER boxes with the actual merging networks. The depth of each comparator is indicated, and sample zero-one values are shown on the wires.

Figure 27.12(a) shows the recursive construction of SORTER[*n*]. The *n* input elements are sorted by using two copies of SORTER[*n*/2] recursively to sort (in parallel) two subsequences of length *n*/2 each. The two resulting sequences are then merged by MERGER[*n*]. The boundary case for the recursion is when *n* = 1, in which case we can use a wire to sort the 1-element sequence, since a 1-element sequence is already sorted. Figure 27.12(b) shows the result of unrolling the recursion, and Figure 27.12(c) shows the actual network obtained by replacing the MERGER boxes in Figure 27.12(b) with the actual merging networks.

Data pass through lg *n* stages in the network SORTER[*n*]. Each of the individual inputs to the network is already a sorted 1-element sequence. The first stage of SORTER[*n*] consists of *n*/2 copies of MERGER[2] that work in parallel to merge pairs of 1-element sequences to produce sorted sequences of length 2. The second stage consists of *n*/4 copies of MERGER[4] that merge pairs of these 2-element sorted sequences to produce sorted sequences of length 4. In general, for *k* = 1, 2,..., lg *n*, stage *k* consists of *n*/2^{k} copies of MERGER[2^{k}] that merge pairs of the 2^{k-1}-element sorted sequences to produce sorted sequences of length 2^{k}. At the final stage, one sorted sequence consisting of all the input values is produced. This sorting network can be shown by induction to sort zero-one sequences, and consequently, by the zero-one principle, it can sort arbitrary values.

We can analyze the depth of the sorting network recursively. The depth *D*(*n*) of SORTER[*n*] is the depth *D*(*n*/2) of SORTER[*n*/2] (there are two copies of SORTER[*n*/2], but they operate in parallel) plus the depth lg *n* of MERGER[*n*]. Consequently, the depth of SORTER[*n*] is given by the recurrence

whose solution is *D*(*n*) = Θ(lg^{2} *n*). Thus, we can sort *n* numbers in parallel in *O*(lg^{2} *n*) time.