23
CS 584

CS 584

  • Upload
    alexa

  • View
    51

  • Download
    0

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

Page 1: CS 584

CS 584

Page 2: 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)

Page 3: CS 584

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.

Page 4: CS 584

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.)

Page 5: CS 584

Compare-Exchange

Page 6: CS 584

Compare-Split

Page 7: CS 584

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}

Page 8: CS 584

Sorting Network

Page 9: CS 584

Parallel Sorting Algorithms

Merge SortMerge Sort Quick SortQuick Sort Bitonic SortBitonic Sort Others … Others …

Page 10: CS 584

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

Page 11: CS 584

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.

Page 12: CS 584

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

Page 13: CS 584

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

Page 14: CS 584

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.

Page 15: CS 584

Rearranging a Bitonic Sequence

Page 16: CS 584

Bitonic Merging Network

Page 17: CS 584

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

Page 18: CS 584

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

Page 19: CS 584

Bitonic Merging Network

Page 20: CS 584

Bitonic Merge on Hypercube

Page 21: CS 584

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

Page 22: CS 584

Bitonic Sort Stages

Page 23: CS 584

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.