Intro to Algorithms: CHAPTER 28: SORTING NETWORKS
In Part II, we examined sorting algorithms for serial computers (random-access machines, or RAM's) that allow only one operation to be executed at a time. In this chapter, we investigate sorting algorithms based on a comparison network model of computation in which many comparison operations can be performed simultaneously.

Comparison networks differ from RAM's in two important respects. First, they can only perform comparisons. Thus, an algorithm such as counting sort (see Section 9.2) cannot be implemented on a comparison network. Second, unlike the RAM model, in which operations occur serially--that is, one after another--operations in a comparison network may occur at the same time, or "in parallel." As we shall see, this characteristic allows the construction of comparison networks that sort *n* values in sublinear time.

We begin in Section 28.1 by defining comparison networks and sorting networks. We also give a natural definition for the "running time" of a comparison network in terms of the depth of the network. Section 28.2 proves the "zero-one principle," which greatly eases the task of analyzing the correctness of sorting networks.

The efficient sorting network that we shall design is essentially a parallel version of the merge-sort algorithm from Section 1.3.1. Our construction will have three steps. Section 28.3 presents the design of a "bitonic" sorter that will be our basic building block. We modify the bitonic sorter slightly in Section 28.4 to produce a merging network that can merge two sorted sequences into one sorted sequence. Finally, in Section 28.5, we assemble these merging networks into a sorting network that can sort *n* values in *O*(lg^{2} *n*)* *time.

Sorting networks are comparison networks that always sort their inputs, so it makes sense to begin our discussion with comparison networks and their characteristics. A comparison network is comprised solely of wires and comparators. A **comparator**, shown in Figure 28.1 (a), is a device with two inputs, *x* and *y*, and two outputs, *x*' and *y*', that performs the following function:

*x*' = min(*x, y*) ,

*y*' = max(*x, y*) .

Because the pictorial representation of a comparator in Figure 28.1 (a) is too bulky for our purposes, we shall adopt the convention of drawing comparators as single vertical lines, as shown in Figure 28.1(b). Inputs appear on the left and outputs on the right, with the smaller input value appearing on the top output and the larger input value appearing on the bottom output. We can thus think of a comparator as sorting its two inputs.

We shall assume that each comparator operates in *O*(1) time. In other words, we assume that the time between the appearance of the input values *x* and *y* and the production of the output values *x*' and *y*' is a constant.

A **wire** transmits a value from place to place. Wires can connect the output of one comparator to the input of another, but otherwise they are either network input wires or network output wires. Throughout this chapter, we shall assume that a comparison network contains *n* **input wires*** a*_{1}, *a*_{2}, . . . , *a*_{n}, through which the values to be sorted enter the network, and *n* **output wires** *b*_{1}, *b*_{2}, . . . , *b*_{n}, which produce the results computed by the network. Also, we shall speak of the **input sequence** *a*_{1}, *a*_{2}, . . . , *a*_{n} and the **output sequence** *b*_{1},*b*_{2, . . . ,}*b*_{n}, referring to the values on the input and output wires. That is, we use the same name for both a wire and the value it carries. Our intention will always be clear from the context.

Figure 28.2 shows a **comparison network**, which is a set of comparators interconnected by wires. We draw a comparison network on *n* inputs as a collection of *n* horizontal **lines** with comparators stretched vertically. Note that a line does *not* represent a single wire, but rather a sequence of distinct wires connecting various comparators. The top line in Figure 28.2, for example, represents three wires: input wire *a*_{1}, which connects to an input of comparator *A*; a wire connecting the top output of comparator *A* to an input of comparator *C*; and output wire *b*_{1}, which comes from the top output of comparator *C*. Each comparator input is connected to a wire that is either one of the network's *n* input wires *a*_{1}, *a*_{2}, . . . , *a*_{n} or is connected to the output of another comparator. Similarly, each comparator output is connected to a wire that is either one of the network's *n* output wires *b*_{1}, *b*_{2}, . . . , *b*_{n} or is connected to the input of another comparator. The main requirement for interconnecting comparators is that the graph of interconnections must be acyclic: if we trace a path from the output of a given comparator to the input of another to output to input, etc., the path we trace must never cycle back on itself and go through the same comparator twice. Thus, as in Figure 28.2, we can draw a comparison network with network inputs on the left and network outputs on the right; data move through the network from left to right.

Each comparator produces its output values only when both of its input values are available to it. In Figure 28.2(a), for example, suppose that the sequence 9, 5, 2, 6 appears on the input wires at time 0. At time 0, then, only comparators *A* and *B* have all their input values available. Assuming that each comparator requires one time unit to compute its output values, comparators *A* and *B* produce their outputs at time 1; the resulting values are shown in Figure 28.2(b). Note that comparators *A* and *B* produce their values at the same time, or "in parallel." Now, at time 1, comparators *C *and *D*, but not *E*, have all their input values available. One time unit later, at time 2, they produce their outputs, as shown in Figure 28.2(c). Comparators *C* and *D* operate in parallel as well. The top output of comparator *C* and the bottom output of comparator *D* connect to output wires *b*_{1} and *b*_{4}, respectively, of the comparison network, and these network output wires therefore carry their final values at time 2. Meanwhile, at time 2, comparator *E* has its inputs available, and Figure 28.2(d) shows that it produces its output values at time 3. These values are carried on network output wires *b*_{2} and *b*_{3}, and the output sequence 2, 5, 6, 9 is now complete.

Under the assumption that each comparator takes unit time, we can define the "running time" of a comparison network, that is, the time it takes for all the output wires to receive their values once the input wires receive theirs. Informally, this time is the largest number of comparators that any input element can pass through as it travels from an input wire to an output wire. More formally, we define the **depth** of a wire as follows. An input wire of a comparison network has depth 0. Now, if a comparator has two input wires with depths *d*_{x} and *d*_{y}, then its output wires have depth max(*d*_{x}, *d*_{y}) + 1. Because there are no cycles of comparators in a comparison network, the depth of a wire is well defined, and we define the depth of a comparator to be the depth of its output wires. Figure 28.2 shows comparator depths. The depth of a comparison network is the maximum depth of an output wire or, equivalently, the maximum depth of a comparator. The comparison network of Figure 28.2, for example, has depth 3 because comparator *E* has depth 3. If each comparator takes one time unit to produce its output value, and if network inputs appear at time 0, a comparator at depth *d* produces its outputs at time *d*; the depth of the network therefore equals the time for the network to produce values at all of its output wires.

A **sorting network** is a comparison network for which the output sequence is monotonically increasing (that is, *b*_{1} *b*_{2} . . . *b*_{n}) for *every* input sequence. Of course, not every comparison network is a sorting network, but the network of Figure 28.2 is. To see why, observe that after time 1, the minimum of the four input values has been produced by either the top output of comparator *A* or the top output of comparator *B*. After time 2, therefore, it must be on the top output of comparator *C*. A symmetrical argument shows that after time 2, the maximum of the four input values has been produced by the bottom output of comparator *D*. All that remains is for comparator *E *to ensure that the middle two values occupy their correct output positions, which happens at time 3.

A comparison network is like a procedure in that it specifies how comparisons are to occur, but it is unlike a procedure in that its physical size depends on the number of inputs and outputs. Therefore, we shall actually be describing "families" of comparison networks. For example, the goal of this chapter is to develop a family SORTER of efficient sorting networks. We specify a given network within a family