# Comparison Networks

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*and

_{x}*d*, then its output wires have depth max(

_{y}*d*,

_{x}*d*) 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 27.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 27.2, for example, has depth 3 because comparator

_{y}*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*) for

_{n}*every*input sequence. Of course, not every comparison network is a sorting network, but the network of Figure 27.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 ** size**-the number of comparators that it contains-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 by the family name and the number of inputs (which equals the number of outputs). For example, the

*n*-input,

*n*-output sorting network in the family SORTER is named SORTER[

*n*].