apr25.txt ------------------------ Back to our ``discovery'' of Batcher's *bitonic* merger: All n/2 spans (necessarily independent). Continue recursively on upper half and lower half. Again clearly O(log n) time, O(n log n) comparators. Nice surprise: If the initial first and second halves are sorted, *but in opposite directions*, then the ultimate result is sorted! Why?? Text's approach: Use zero-one principle and ``cleanliness.'' (Rough correspondence with our terms: 0 means ``small''; 1 means ``large''; clean means all small or all large.) Back to our more direct approach: Looking for a generalization ``bitonic'' of ``both halves sorted, but in opposite directions,'' such that two lemmas hold: Lemma 1: After first round of comparisons, all in first half are < all in second half. Lemma 2: After first round of comparisons, each half is itself bitonic. Proof of Lemma 1: Consider lining up the compared elements in the first and second halves. Note that there is a unique ``crossover'' line between results < and results >. 1st half 2nd half ``small'' < ``large'' - - - - - - - - - - - - - ``large'' > ``small'' Note that every ``small'' is less than every ``large.'' Therefore, the dictated switches will indeed segregate. On the other hand, Lemma *2* is false! (Exercise.) ------------------------ Patch: Rather than tweak the *algorithm*, need only generalize the (inductive) *claim*: First try: Original sequence is concatenation of two sequences, both sorted, but in opposite directions. (Resulting bonus: Then our network will also be usable for merging sorted lists of two different sizes.) Still get Lemma 1? Yes--draw picture again. < more so upward, > more so downward. But: Lemma 2 *still* fails! (Exercise.) ------------------------ Final patch: Allow also cyclic permutation of above notion. (Call such a sequence ``bitonic.'') I.e., above notion (concatenation of two oppositely sorted sequences) ``on a circle.'' Note what happens as you cyclically permute the input: Each moved pair from top gets put on bottom in opposite order. Therefore, continue to have unique crossover. And continue to have Lemma 1, because each of the resulting halves gets the same results, but themselves cyclically permuted. Proof of Lemma 2, at last: By observation above, enough to observe this in the nice case of concatenation of an increasing sequence and a decreasing one. Wlog, assume the former is shorter, and look at the picture. The resulting left half is a concatenation of the two ``small'' blocks, and hence a cyclical permutation of a cyclically contiguous subsequence of the original sequence and still bitonic. Similarly for the resulting right half, a concatenation of the two ``large'' blocks. ------------------------ Possible to convert the bitonic sorter to a merging network (which receives boths halves sorted in same direction)? Two ways: 1. Mindless: ``Reverse the order of the second half.'' I.e., appropriately redraw (and ``redirect''!) every comparator. (Result is still oblivious, but not quite a ``comparison network.'') Then cite (2nd Edition) Exercise 27.1-8. 2. More insightful: Just redraw the comparators for the very first stage. Each virtual half of the results will then be bitonic, by Lemma 2. The first virtual half is the first real half, but the second virtual half is the *reverse* of the first real half. Insight: The reverse of a bitonic sequence is also bitonic! So continue *exactly* as in our bitonic sorting algorithm. ------------------------ Finally, clear how to use (any) merging network to get a sorting network? (Exactly the familiar mergesort approach.) Depth? lg n stages, each of depth at most lg n. Size? Each stage at most O(n lg n). (See this?) ------------------------ Next: Maybe some omissions, such as Peterson's protocol for leader selection on unidirectional ring. (And address HW questions!)