Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Similar presentations


Presentation on theme: "Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort."— Presentation transcript:

1 Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort

2 Rank Sort for(i=0 ; i<n ; i++){ x=0; for(j=0 ; j<n ; j++) if( a[i]>a[j]) x++; b[x] = a[i]; } O(n 2 )

3 Rank Sort Using n processors forall(i=0 ; i<n ; i++){ x=0; for(j=0 ; j<n ; j++) if(a[i] > a[j] ) x++: b[x] = a[i]; } O(n)

4 Rank Sort Using n 2 processors O(1)

5 Rank Sort

6 Compare-and-Exchange if(A > B) { temp = A; A = B; B = temp; }

7 Compare-and-Exchange Process P1 send(&A, P2); recv(&A, P2); Process P2 recv(&A, P1); if(A > B){ send(&B, P1); B = A; }else send(&A, P1);

8 Compare-and-Exchange Process P1 send(&A, P2); recv(&B, P2); if(A > B) A = B; Process P2 recv(&A, P1); send(&B, P1); if(A > B) B = A;

9 Compare-and-Exchange Data partitioning

10 Compare-and-Exchange Data partitioning

11 Bubble Sort and Odd-even Transposition Sort

12 for(i=n-1 ; i > 0 ; i++) for(j=0 ; j < i ; j++){ k = j+1; if( a[j] < a[k]) { temp = a[j]; a[i] = a[k]; a[k] = temp; } O(n 2 )

13 Bubble Sort and Odd-even Transposition Sort Parallel Code

14 Bubble Sort and Odd-even Transposition Sort

15 Pi,i=0,2,4,6,...,n-2(even) recv(&A, Pi+1); send(&B, Pi+1); if(A > B) B = A; Pi,i=1,3,5,..,n-1(odd) send(&A,Pi-1); recv(&B, Pi-1); if(A > B) A = B;

16 Bubble Sort and Odd-even Transposition Sort Pi,i=1,3,5,...,n-3(odd) send(&A, Pi+1); recv(&B, Pi+1); if(A > B) A = B; Pi,i=2,4,6,...,n-2(even) recv(&A, Pi-1); send(&B, Pi-1); if(A > B) B = A;

17 Bubble Sort and Odd-even Transposition Sort Pi,i=0,2,4,...,n-1(odd) send(&A, Pi-1); recv(&B, Pi-1); if(A > B) A=B; if(i<=n-3){ send(&A, Pi+1); recv(&B, Pi+1); if(A < B) A=B; } Pi,i=0,2,4,...,n-2(even) recv(&A, Pi+1); send(&B, Pi+1); if(A > B) B = A; if(i >= 2){ recv(&A, Pi-1); send(&B, Pi-1); if(A > B) B=A; }

18 Bubble Sort and Odd-even Transposition Sort Two-Dimension Sorting

19 Bubble Sort and Odd-even Transposition Sort Odd Phase, the following actios are done: Each row of numbers is sorted independently, in alternative directions: Even rows: The smallest number of each column is placed at rightmost end and largest number at the leftmost end Odd rows: The smallest number of each column is placed at the leftmost end and the largest number at the rightmost end. In even phase, the following actions are done: Each column of numbers is sorted independently, placing the smallest number of each column at the leftmost end and the largest number at the rightmost end.

20 Bubble Sort and Odd-even Transposition Sort

21

22 Merge Sort

23 Communication (division phase) Communication at each step Processor communication P0->P4 P0->P2;P4->P6 P0->P1;P2->P3;P4->P5;P6->P7

24 Merge Sort Communication (merge phase) Communication at each step Processor communication P0->P4 P0->P2;P4->P6 P0->P1;P2->P3;P4->P5;P6->P7

25 Merge Sort Communication

26 Merge Sort Computation P0 P0;P4 P0;P2;P4;P6 The parallel computational time complexity is O(p) using p processors and one number in each processor

27 Quick Sort quicksort(list, start, end) { if(start < end) partition(list, start, end pivot); quicksort(list, start, pivot-1); quicksort(list, pivot-1, end); }

28 Quick Sort

29

30 Computation Communication

31 Quick Sort Implementation

32 Quick Sort on Hypercube Complete list in one processor 1st step: 000 -> 100 (numbers greater than a pivot, say p1) 2nd step: 000 -> 010 (numbers greater than a pivot, say p2) 100 -> 110 (numbers greater than a pivot, say p3) 3rd step: 000 -> 001 (numbers greater than a pivot, say p4) 010 -> 011 (numbers greater than a pivot, say p5) 100 -> 101 (numbers greater than a pviot, say p6) 110 -> 111 (numbers greater than a pivot, say p7)

33 Quick Sort on Hypercube

34 Number initially distributed across all processors 1. one processor(say P0) selects (or computers) a suitable pivot and broadcast this to all others in the cube 2. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube send their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 3. Each processor concatenates the list received with what remains of its own list.

35 Quick Sort on Hypercube

36

37 1. Each processor sorts its list sequentially. 2. one processor(say P0) selects (or computers) a suitable pivot and broadcast this to all others in the cube 3. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube sned their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 4. Each processor merger the list received with its own to obtain a sorted list.

38 Quick Sort on Hypercube

39 Computation-Pivot Selection O(1) : the (n/2p)th number Communication-Pivot broadcast Computation-Data split if the numbers are sorted and there are x numbers, the split operation can be done in logx steps. (same as binary search) Communication-Data from split Computation-Data Merge to merge two sorted lists into one sorted list requires x steps if the biggest list has x numbers

40 Odd-even Merge Sort 1.The elements with odd indices of each sequence-that is, A1, A3,A5, …,An-1, and B1, B3,B5, …,Bn-1---are merged into one sorted list, C1, C2, C3, …,Cn 2.The elements with even indices of each sequence---that is A2,A4,A6, …,An, and B2,B4,B6, …,Bn-2---are merged into one sorted list, D1, D2, D3, …,Dn. 3.The final sorted list, E1, E2, …,E2n, is obtained by the following: E2i=min{Ci+1, Di} E2i+1=max{Ci+1,Di} for 1<=i<=n-1

41 Odd-even Merge Sort

42

43 Bitonic Merge Sort Bitonic sequence A0 Ai+1>Ai+2> …..>An 3,5,8,9,7,4,2,1

44 Bitonic Merge Sort

45

46

47 Phase 1(step1) Covert pairs of numbers into increasing/decreasing sequences and hence into 4-bit bitonic sequences Phase 2(step2/3) Split each 4-bit bitonic sequence into two 2-bit bitonic sequences, higher sequence at center. Sort each 4-bit bitonic sequence increasing/decreasing sequences and merge into 8-bit bitonic sequence. Phase 3(step4/5/6) Sort 8-bit bitonic sequence

48 Bitonic Merge Sort

49


Download ppt "Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort."

Similar presentations


Ads by Google