A NETWORK FOR SORTING

4.2 A NETWORK FOR SORTING

Recall how an (r, network was constructed in section 3.2 for merging two

sorted sequences. It is rather straightforward to use a collection of merging networks to build a sorting network for the sequence =

... , where n is a power of 2.

The idea is the following. In a first stage, a rank of comparators is used to create

n/2 sorted sequences each of length 2. In a second stage, pairs of these are now merged into sorted sequences of length 4 using a rank of

networks. Again, in a third stage, pairs of sequences of length 4 are merged using

networks

into sequences of length 8. The process continues until two sequences of length each are merged by an

network to produce a single sorted sequence of length n. The resulting architecture is known as an odd-even sorting

and is illustrated in Fig. 4.1 for S =

Note that, as in the case of merging, the odd-even sorting network is oblivious of its input.

Analysis. As we did for the merging network, we shall analyze the running time, number of comparators, and cost of the odd-even sorting network. Since the size of the merged sequences doubles after every stage, there are log n stages in all.

( i ) Running Time. Denote by the time required in the ith stage to merge two sorted sequences of

elements each. From section 3.2 we have the recurrence

= 1 for i = 1,

+ 1 for i > 1,

whose solution is = i. Therefore, the time required by an odd-even sorting network to sort a sequence of length n is

O(log 2 n).

Note that this is significantly faster than the (optimal) sequential running time of O(n log n) achieved by procedure QUICKSORT.

(ii) Number of Processors. Denote by the number of comparators required in the ith stage to merge two sorted sequences of

elements each. From section 3.2 we have the recurrence

for i =

1 for i > 1,

whose solution is

= (i -

+ 1. Therefore, the number of comparators

4.3 Sorting on a Linear Array needed by an odd-even sorting network to sort a sequence of length n is

= O(n log 2 n), the total number of comparisons performed by an odd-even sorting network, that is, the network's cost, is

(iii) Cost. Since

= O(log 2 n) and

Our sorting network is therefore not cost optimal as it performs more operations than the O(n log n) sufficient to sort sequentially.

Since the odd-even sorting network is based on the odd-even merging one, the remarks made in section 3.2 apply here as well. In particular:

(i) The network is extremely fast. It can sort a sequence of length within, on the order of,

time units. This is to be contrasted with the time required by procedure QUICKSORT, which would be in excess of

20 million time units. (ii) The number of comparators is too high. Again for n =

the network would need on the order of 400 million comparators. (iii) The architecture is highly irregular and the wires linking the comparators have lengths that vary with n.

We therefore reach the same conclusion as for the merging network of section

3.2: The odd-even sorting network is impractical for large input sequences.