Upload
alexa
View
51
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
CS 584
Sorting
One of the most common operationsOne of the most common operations Definition:Definition:
– Arrange an unordered collection of Arrange an unordered collection of elements into a monotonically increasing or elements into a monotonically increasing or decreasing order.decreasing order.
Two categories of sortingTwo categories of sorting– internal (fits in memory)internal (fits in memory)– external (uses auxiliary storage)external (uses auxiliary storage)
Sorting Algorithms
Comparison basedComparison based– compare-exchangecompare-exchange– O(n log n)O(n log n)
Noncomparison basedNoncomparison based– Uses known properties of the elementsUses known properties of the elements– O(n)O(n) - bucket sort etc. - bucket sort etc.
Parallel Sorting Issues
Input and Output sequence storageInput and Output sequence storage– Where?Where?– Local to one processor or distributedLocal to one processor or distributed
ComparisonsComparisons– How compare elements on different nodesHow compare elements on different nodes
# of elements per processor# of elements per processor– One (compare-exchange --> comm.)One (compare-exchange --> comm.)– Multiple (compare-split --> comm.)Multiple (compare-split --> comm.)
Compare-Exchange
Compare-Split
Sorting Networks
Specialized hardware for sortingSpecialized hardware for sorting– based on comparatorbased on comparator
xy
xy
max{x,y}min{x,y}
min{x,y}max{x,y}
Sorting Network
Parallel Sorting Algorithms
Merge SortMerge Sort Quick SortQuick Sort Bitonic SortBitonic Sort Others … Others …
Merge Sort
Simplest parallel sorting algorithm?Simplest parallel sorting algorithm? StepsSteps
– Distribute the elementsDistribute the elements– Everybody sort their own sequenceEverybody sort their own sequence– Merge the listsMerge the lists
ProblemProblem– How to merge the listsHow to merge the lists
Bitonic Sort
Key operation:Key operation:– rearrange a bitonic sequence to orderedrearrange a bitonic sequence to ordered
Bitonic SequenceBitonic Sequence– sequence of elements <asequence of elements <a00, a, a11, … , a, … , an-1n-1>>
There exists i such that <aThere exists i such that <a00, … ,a, … ,aii> is > is monotonically increasing and <amonotonically increasing and <a i+1i+1,… , a,… , an-1n-1> is > is monotonically decreasing ormonotonically decreasing or
There exists a cyclic shift of indicies such that the There exists a cyclic shift of indicies such that the above is satisfied.above is satisfied.
Bitonic Sequences
<1, 2, 4, 7, 6, 0> <1, 2, 4, 7, 6, 0> – First it increases then decreasesFirst it increases then decreases– i = 3i = 3
<8, 9, 2, 1, 0, 4><8, 9, 2, 1, 0, 4>– Consider a cyclic shiftConsider a cyclic shift– i will equal 3i will equal 3
Rearranging a Bitonic Sequence Let s = <aLet s = <a00, a, a11, … , a, … , an-1n-1>>
– aan/2n/2 is the beginning of the decreasing seq. is the beginning of the decreasing seq. Let sLet s11= <min{a= <min{a00, a, an/2n/2}, min{a}, min{a11, a, an/2 +1n/2 +1}…min{a}…min{an/2-1n/2-1,a,an-1n-1}>}> Let sLet s22=<max{a=<max{a00, a, an/2n/2}, max{a}, max{a11,a,an/2+1n/2+1}… max{a}… max{an/2-1n/2-1,a,an-1n-1} >} > In sequence sIn sequence s11 there is an element b there is an element bii = min{a = min{aii, a, an/2+in/2+i}}
– all elements before ball elements before bii are from increasing are from increasing
– all elements after ball elements after bii are from decreasing are from decreasing Sequence sSequence s22 has a similar point has a similar point Sequences sSequences s11 and s and s22 are bitonic are bitonic
Rearranging a Bitonic Sequence
Every element of sEvery element of s11 is smaller than is smaller than every element of severy element of s22
Thus, we have reduced the problem of Thus, we have reduced the problem of rearranging a bitonic sequence of size n rearranging a bitonic sequence of size n to rearranging two bitonic sequences of to rearranging two bitonic sequences of size n/2 then concatenating the size n/2 then concatenating the sequences.sequences.
Rearranging a Bitonic Sequence
Bitonic Merging Network
What about unordered lists?
To use the bitonic merge for n items, we must To use the bitonic merge for n items, we must first have a bitonic sequence of n items.first have a bitonic sequence of n items.
Two elements form a bitonic sequenceTwo elements form a bitonic sequence Any unsorted sequence is a concatenation of Any unsorted sequence is a concatenation of
bitonic sequences of size 2bitonic sequences of size 2 Merge those into larger bitonic sequences Merge those into larger bitonic sequences
until we end up with a bitonic sequence of until we end up with a bitonic sequence of size nsize n
Mapping onto a hypercube
One element per processorOne element per processor Start with the sorting network mapsStart with the sorting network maps Each wire represents a processorEach wire represents a processor Map processors to wires to minimize Map processors to wires to minimize
the distance traveled during exchangethe distance traveled during exchange
Bitonic Merging Network
Bitonic Merge on Hypercube
Bitonic SortProcedure BitonicSortfor 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
endforendfor
comp_exchange_max and comp_exchange_min compare and exchange the item with the neighbor on the jth dimension
Bitonic Sort Stages
Assignment
Pick 16 random integersPick 16 random integers Draw the Bitonic Sort networkDraw the Bitonic Sort network Step through the Bitonic sort network to Step through the Bitonic sort network to
produce a sorted list of integers.produce a sorted list of integers. Explain how the if statement in the Explain how the if statement in the
Bitonic sort algorithm works.Bitonic sort algorithm works.