1 / 23

CS 584

CS 584. Sorting. One of the most common operations Definition: Arrange an unordered collection of elements into a monotonically increasing or decreasing order. Two categories of sorting internal (fits in memory) external (uses auxiliary storage). Sorting Algorithms. Comparison based

Download Presentation

CS 584

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 584

  2. Sorting • One of the most common operations • Definition: • Arrange an unordered collection of elements into a monotonically increasing or decreasing order. • Two categories of sorting • internal (fits in memory) • external (uses auxiliary storage)

  3. Sorting Algorithms • Comparison based • compare-exchange • O(n log n) • Noncomparison based • Uses known properties of the elements • O(n) - bucket sort etc.

  4. Parallel Sorting Issues • Input and Output sequence storage • Where? • Local to one processor or distributed • Comparisons • How compare elements on different nodes • # of elements per processor • One (compare-exchange --> comm.) • Multiple (compare-split --> comm.)

  5. Compare-Exchange

  6. Compare-Split

  7. Sorting Networks • Specialized hardware for sorting • based on comparator x y x y max{x,y} min{x,y} min{x,y} max{x,y}

  8. Sorting Network

  9. Parallel Sorting Algorithms • Merge Sort • Quick Sort • Bitonic Sort • Others …

  10. Merge Sort • Simplest parallel sorting algorithm? • Steps • Distribute the elements • Everybody sort their own sequence • Merge the lists • Problem • How to merge the lists

  11. Bitonic Sort • Key operation: • rearrange a bitonic sequence to ordered • Bitonic Sequence • sequence of elements <a0, a1, … , an-1> • There exists i such that <a0, … ,ai> is monotonically increasing and <ai+1,… , an-1> is monotonically decreasing or • There exists a cyclic shift of indicies such that the above is satisfied.

  12. Bitonic Sequences • <1, 2, 4, 7, 6, 0> • First it increases then decreases • i = 3 • <8, 9, 2, 1, 0, 4> • Consider a cyclic shift • i will equal 3

  13. Rearranging a Bitonic Sequence • Let s = <a0, a1, … , an-1> • an/2 is the beginning of the decreasing seq. • Let s1= <min{a0, an/2}, min{a1, an/2 +1}…min{an/2-1,an-1}> • Let s2=<max{a0, an/2}, max{a1,an/2+1}… max{an/2-1,an-1} > • In sequence s1 there is an element bi = min{ai, an/2+i} • all elements before bi are from increasing • all elements after bi are from decreasing • Sequence s2 has a similar point • Sequences s1 and s2 are bitonic

  14. Rearranging a Bitonic Sequence • Every element of s1 is smaller than every element of s2 • Thus, we have reduced the problem of rearranging a bitonic sequence of size n to rearranging two bitonic sequences of size n/2 then concatenating the sequences.

  15. Rearranging a Bitonic Sequence

  16. Bitonic Merging Network

  17. What about unordered lists? • To use the bitonic merge for n items, we must first have a bitonic sequence of n items. • Two elements form a bitonic sequence • Any unsorted sequence is a concatenation of bitonic sequences of size 2 • Merge those into larger bitonic sequences until we end up with a bitonic sequence of size n

  18. Mapping onto a hypercube • One element per processor • Start with the sorting network maps • Each wire represents a processor • Map processors to wires to minimize the distance traveled during exchange

  19. Bitonic Merging Network

  20. Bitonic Merge on Hypercube

  21. Bitonic Sort Procedure BitonicSort for i = 0 to d -1 for j = i downto 0 if (i + 1)st bit of iproc <> jth bit of iproc comp_exchange_max(j, item) else comp_exchange_min(j, item) endif endfor endfor comp_exchange_max and comp_exchange_min compare and exchange the item with the neighbor on the jth dimension

  22. Bitonic Sort Stages

  23. Assignment • Pick 16 random integers • Draw the Bitonic Sort network • Step through the Bitonic sort network to produce a sorted list of integers. • Explain how the if statement in the Bitonic sort algorithm works.

More Related