-
Notifications
You must be signed in to change notification settings - Fork 5
/
quicksort.cpp
56 lines (48 loc) · 926 Bytes
/
quicksort.cpp
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
#include "quicksort.h"
#include "helper.h"
using namespace std;
int splitq ( dataType *a, int upper ){
int p, q;
p = 0 ;
q = upper - 1 ;
while ( q >= p ){
while (q>=p && getkey(a,p) <= getkey(a,0) )
p++ ;
while (q>=p && getkey(a,q) > getkey(a,0) )
q-- ;
if ( q > p )
swap(a,p,q);
}
swap(a,0,q);
return q ;
}
void qsort(dataType *data, int start, int end){
if (start+1>=end){
return;
} else if (start+2==end){
if ( getkey(data,start)> getkey(data,start+1)){
swap(data, start, start+1);
}
}
else if (start+1<end){
int size = end - start;
int store = start + splitq(data+start, size);
#pragma omp task if (size > 1 <<20)
{
qsort(data, start, store);
}
#pragma omp task if (size > 1 <<20)
{
qsort(data, store+1, end);
}
}
}
void quicksort(dataType *data, int size){
#pragma omp parallel
{
#pragma omp single nowait
{
qsort(data, 0, size);
}
}
}