1 procedure bitonic sort label d 2 begin 3 for i 0 to d 1
play

1. procedure BITONIC SORT ( label , d ) 2. begin 3. for i := 0 to - PDF document

1. procedure BITONIC SORT ( label , d ) 2. begin 3. for i := 0 to d 1 do 4. for j := i downto 0 do if ( i + 1 ) st bit of label = j th bit of label then 5. 6. comp exchange max(j) ; 7. else 8. comp exchange min(j) ; 9. end BITONIC


  1. 1. procedure BITONIC SORT ( label , d ) 2. begin 3. for i := 0 to d − 1 do 4. for j := i downto 0 do if ( i + 1 ) st bit of label �= j th bit of label then 5. 6. comp exchange max(j) ; 7. else 8. comp exchange min(j) ; 9. end BITONIC SORT Parallel formulation of bitonic sort on a hypercube with n = 2 d processes. In this Algorithm 9.1 algorithm, label is the process’s label and d is the dimension of the hypercube.

  2. 1. procedure BUBBLE SORT ( n ) 2. begin 3. for i := n − 1 downto 1 do 4. for j := 1 to i do 5. compare-exchange ( a j , a j + 1 ) ; 6. end BUBBLE SORT Algorithm 9.2 Sequential bubble sort algorithm.

  3. 1. procedure ODD-EVEN ( n ) begin 2. 3. for i := 1 to n do 4. begin 5. if i is odd then 6. for j := 0 to n / 2 − 1 do 7. compare-exchange ( a 2 j + 1 , a 2 j + 2 ) ; 8. if i is even then 9. for j := 1 to n / 2 − 1 do 10. compare-exchange ( a 2 j , a 2 j + 1 ) ; 11. end for 12. end ODD-EVEN Algorithm 9.3 Sequential odd-even transposition sort algorithm.

  4. 1. procedure ODD-EVEN PAR ( n ) 2. begin 3. id := process’s label 4. for i := 1 to n do begin 5. 6. if i is odd then 7. if id is odd then 8. compare-exchange min ( id + 1 ) ; 9. else 10. compare-exchange max ( id − 1 ) ; 11. if i is even then 12. if id is even then 13. compare-exchange min ( id + 1 ) ; 14. else 15. compare-exchange max ( id − 1 ) ; 16. end for 17. end ODD-EVEN PAR Algorithm 9.4 The parallel formulation of odd-even transposition sort on an n -process ring.

  5. 1. procedure QUICKSORT ( A , q , r ) 2. begin 3. if q < r then 4. begin 5. x := A [ q ] ; 6. s := q ; 7. for i := q + 1 to r do 8. if A [ i ] ≤ x then 9. begin 10. s := s + 1; 11. swap( A [ s ] , A [ i ] ); 12. end if 13. swap( A [ q ] , A [ s ] ); 14. QUICKSORT ( A , q , s ) ; 15. QUICKSORT ( A , s + 1 , r ) ; 16. end if 17. end QUICKSORT Algorithm 9.5 The sequential quicksort algorithm.

  6. 1. procedure BUILD TREE ( A [ 1 . . . n ] ) 2. begin 3. for each process i do 4. begin 5. root := i ; 6. parent i := root ; 7. leftchild [ i ] := rightchild [ i ] := n + 1; 8. end for repeat for each process i �= root do 9. 10. begin 11. if ( A [ i ] < A [ parent i ] ) or ( A [ i ] = A [ parent i ] and i < parent i ) then 12. begin 13. leftchild [ parent i ] := i ; 14. if i = leftchild [ parent i ] then exit 15. else parent i := leftchild [ parent i ] ; 16. end for 17. else 18. begin 19. rightchild [ parent i ] := i ; 20. if i = rightchild [ parent i ] then exit 21. else parent i := rightchild [ parent i ] ; 22. end else 23. end repeat 24. end BUILD TREE Algorithm 9.6 The binary tree construction procedure for the CRCW PRAM parallel quicksort formulation.

  7. 1. procedure ENUM SORT ( n ) begin 2. for each process P 1 , j do 3. 4. C [ j ] := 0; 5. for each process P i , j do 6. if ( A [ i ] < A [ j ] ) or ( A [ i ] = A [ j ] and i < j ) then 7. C [ j ] := 1; 8. else 9. C [ j ] := 0; 10. for each process P 1 , j do 11. A [ C [ j ]] := A [ j ] ; 12. end ENUM SORT Algorithm 9.7 Enumeration sort on a CRCW PRAM with additive-write conflict resolution.

  8. 1. procedure RADIX SORT ( A , r ) 2. begin 3. for i := 0 to b / r − 1 do 4. begin 5. offset := 0 ; for j := 0 to 2 r − 1 do 6. begin 7. 8. flag := 0 ; if the i th least significant r -bit block of A [ P k ] = j then 9. 10. flag := 1 ; 11. index := prefix sum(flag) 12. if f lag = 1 then 13. rank := offset + index ; 14. offset := parallel sum(flag) ; 15. endfor 16. each process P k send its element A [ P k ] to process P rank ; 17. endfor 18. end RADIX SORT Algorithm 9.8 A parallel radix sort algorithm, in which each element of the array A [ 1 . . . n ] to be sorted is assigned to one process. The function prefix sum() computes the prefix sum of the flag variable, and the function parallel sum() returns the total sum of the flag variable.

  9. 1. procedure HYPERCUBE QUICKSORT ( B , n ) 2. begin 3. id := process’s label; 4. for i := 1 to d do 5. begin 6. x := pivot ; 7. partition B into B 1 and B 2 such that B 1 ≤ x < B 2 ; if i th bit is 0 then 8. 9. begin send B 2 to the process along the i th communication link; 10. C := subsequence received along the i th communication link; 11. 12. B := B 1 ∪ C ; 13. endif 14. else send B 1 to the process along the i th communication link; 15. C := subsequence received along the i th communication link; 16. 17. B := B 2 ∪ C ; 18. endelse 19. endfor 20. sort B using sequential quicksort; 21. end HYPERCUBE QUICKSORT Algorithm 9.9 A parallel formulation of quicksort on a d -dimensional hypercube. B is the n / p - element subsequence assigned to each process.

Recommend


More recommend