< Day Day Up > |
Our sorting network will be constructed from merging networks, which are networks that can merge two sorted input sequences into one sorted output sequence. We modify BITONIC-SORTER[n] to create the merging network MERGER[n]. As with the bitonic sorter, we shall prove the correctness of the merging network only for inputs that are zero-one sequences. Exercise 27.4-1 asks you to show how the proof can be extended to arbitrary input values.
The merging network is based on the following intuition. Given two sorted sequences, if we reverse the order of the second sequence and then concatenate the two sequences, the resulting sequence is bitonic. For example, given the sorted zero-one sequences X = 00000111 and Y = 00001111, we reverse Y to get YR= 11110000. Concatenating X and YR yields 0000011111110000, which is bitonic. Thus, to merge the two input sequences X and Y, it suffices to perform a bitonic sort on X concatenated with YR.
We can construct MERGER[n] by modifying the first half-cleaner of BITONIC- SORTER[n]. The key is to perform the reversal of the second half of the inputs implicitly. Given two sorted sequences 〈a1, a2,...,an/2〉 and 〈an/2+1, an/2+2,..., an〉 to be merged, we want the effect of bitonically sorting the sequence 〈a1, a2,..., an/2, an, an-1,..., an/2+1〉. Since the first half-cleaner of BITONIC-SORTER[n] compares inputs i and n/2 + i, for i = 1, 2,..., n/2, we make the first stage of the merging network compare inputs i and n - i + 1. Figure 27.10 shows the correspondence. The only subtlety is that the order of the outputs from the bottom of the first stage of MERGER[n] are reversed compared with the order of outputs from an ordinary half-cleaner. Since the reversal of a bitonic sequence is bitonic, however, the top and bottom outputs of the first stage of the merging network satisfy the properties in Lemma 27.3, and thus the top and bottom can be bitonically sorted in parallel to produce the sorted output of the merging network.
The resulting merging network is shown in Figure 27.11. Only the first stage of MERGER[n] is different from BITONIC-SORTER[n]. Consequently, the depth of MERGER[n] is lg n, the same as that of BITONIC-SORTER[n].
How many different zero-one input sequences must be applied to the input of a comparison network to verify that it is a merging network?
Show that any network that can merge 1 item with n - 1 sorted items to produce a sorted sequence of length n must have depth at least lg n.
Consider a merging network with inputs a1, a2,..., an, for n an exact power of 2, in which the two monotonic sequences to be merged are 〈a1, a3,..., an-1〉 and 〈a2, a4,..., an〉. Prove that the number of comparators in this kind of merging network is Φ(n lg n). Why is this an interesting lower bound? (Hint: Partition the comparators into three sets.)
< Day Day Up > |