------------------------ A peek at parallel sorting: ``Oblivious'' methods (Variables to be compared-and-maybe-swapped at each time are known ahead of time, regardless of the input data.) AKS sorting O(log n) levels of O(n) comparators (but large constants) --so time O(log n), optimal --beautiful, but too hairy to cover this term Via Batcher ``bitonic'' merging Via Batcher ``odd-even'' merging Both O(log n) levels of O(n) comparators (but nice) ------------------------ Odd-even merging: Reduce to 2 half-size problems (solvable recursively, in parallel): 1. Merge the ``odds'' (1st, 3rd, 5th, ... from each list) 2. Merge the ``evens'' (2nd, 4th, 6th, ... from each list) How finish with just 1 more, oblivious level??! Claim: Enough to shuffle (odd, even, odd, even, ...) and do all the even-odd comparators! (Remark: Wlog, all different, since can include index as low-order key. (Thus, sure to get stable sort.)) (Draw examples for small powers of 2. Note that the shuffles require no work by the comparison networks.) Proof of claim: Idea: Because of origins as odds and evens in the same sorted lists, ``rank among odds approx= rank among evens'' More carefully: Consider the i-th odd; say from first input list (wlog). How many *odds* smaller? i-1; say j from first list k from second. How many *evens* smaller? Exactly j from first list. Either k or k-1 from second list. Therefore, either j+k=i-1 or j+k-1=i-2. Therefore, *total* number smaller is either 2i-2 or 2i-3. Similarly, consider the i-th even; say from first input list (wlog). How many *evens* smaller? i-1; say j from first list, k from second. How many *odds* smaller? Exactly j+1 from first list. Either k or k+1 from second list. Therefore, either j+k+1=i or j+k+2=i+1. Therefore, *total* number smaller is either 2i-1 or 2i. Conclusion: i-th even and (i+1)-st odd have the ranks 2i and 2i-1. ------------------------ Retrospective structure: Some independent n/2 spans Some independent n/4 spans Some independent n/8 spans ... Some independent 2 spans Some independent 1 spans So clearly O(log n) time, O(n log n) comparators. Exercises: Exact number of comparators? Of each span? Simple rule for where to place them? (Easy for n/2, 1.) Clear invariant for each point in time? How does the AKS framework compare with this one? ------------------------ A structurally similar variant: 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?? Obvious induction to try: Lemma 1: After first round of comparisons, all in first half are < all in second half. Lemma 2: After first round of comparisons, each quarter (assuming n >= 4) is sorted, with directions opposite for 1st and 2nd and for 3rd and 4th. 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.)