-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort.chpl
145 lines (124 loc) · 3.93 KB
/
quicksort.chpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// An example of a parallel quick sort implementation that uses
// "cobegin" to make each recursive call in parallel and "serial" to
// limit the number of threads.
//
use Random, Time; // for random number generation and the Timer class
var timer: Timer; // to time the sort
config var n: int = 2**15; // the size of the array to be sorted
config var thresh: int = 1; // the recursive depth to serialize
config var verbose: int = 0; // print out this many elements in array
config var timing: bool = true; // set timing to false to disable timer
var A: [1..n] real; // array of real numbers
//
// initialize array with random numbers
//
fillRandom(A);
//
// print out front of array if verbose flag is set
//
if verbose > 0 then
writeln("A[1..", verbose, "] = ", A[1..verbose]);
//
// start timer, call parallel quick sort routine, stop timer
//
if timing then timer.start();
pqsort(A, thresh);
if timing then timer.stop();
//
// report sort time
//
if timing then writeln("sorted in ", timer.elapsed(), " seconds");
//
// print out front of array if verbose flag is set
// values should now be in sorted order
//
if verbose > 0 then
writeln("A[1..", verbose, "] = ", A[1..verbose]);
//
// verify that array is sorted or halt
//
for i in 2..n do
if A(i) < A(i-1) then
halt("A(", i-1, ") == ", A(i-1), " > A(", i, ") == ", A(i));
writeln("verification success");
//
// pqsort -- parallel quick sort
//
// arr: generic 1D array of values (real, int, ...)
// thresh: number of recursive calls to make before serializing
// low: lower bound of array to start sort at, defaults to whole array
// high: upper bound of array to stop sort at, defaults to whole array
//
proc pqsort(arr: [],
thresh: int,
low: int = arr.domain.low,
high: int = arr.domain.high) where arr.rank == 1 {
//
// base case: arr[low..high] is small enough to bubble sort
//
if high - low < 8 {
bubbleSort(arr, low, high);
return;
}
//
// determine pivot and partition arr[low..high]
//
const pivotVal = findPivot();
const pivotLoc = partition(pivotVal);
//
// make recursive calls to parallel quick sort each unsorted half of
// the array; if thresh is 0 or less, start executing conquer tasks
// serially
//
serial thresh <= 0 do cobegin {
pqsort(arr, thresh-1, low, pivotLoc-1);
pqsort(arr, thresh-1, pivotLoc+1, high);
}
//
// findPivot -- helper routine to find pivot value using simple
// median-of-3 method, returns pivot value
//
proc findPivot() {
const mid = low + (high-low+1) / 2;
if arr(mid) < arr(low) then arr(mid) <=> arr(low);
if arr(high) < arr(low) then arr(high) <=> arr(low);
if arr(high) < arr(mid) then arr(high) <=> arr(mid);
const pivotVal = arr(mid);
arr(mid) = arr(high-1);
arr(high-1) = pivotVal;
return pivotVal;
}
//
// partition -- helper routine to partition array such that all
// values less than pivot are to its left and all
// values greater than pivot are to its right, returns
// pivot location
//
proc partition(pivotVal) {
var ilo = low, ihi = high-1;
while (ilo < ihi) {
do { ilo += 1; } while arr(ilo) < pivotVal;
do { ihi -= 1; } while pivotVal < arr(ihi);
if (ilo < ihi) {
arr(ilo) <=> arr(ihi);
}
}
arr(high-1) = arr(ilo);
arr(ilo) = pivotVal;
return ilo;
}
}
//
// bubbleSort -- bubble sort for base case of quick sort
//
// arr: generic 1D array of values (real, int, ...)
// low: lower bound of array to start sort at
// high: upper bound of array to stop sort at
//
proc bubbleSort(arr: [], low: int, high: int) where arr.rank == 1 {
for i in low..high do
for j in low..high-1 do
if arr(j) > arr(j+1) then
arr(j) <=> arr(j+1);
}