-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.cpp
266 lines (152 loc) · 3.81 KB
/
Quick_Sort.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <iostream>
#include <chrono>
#include <omp.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
using namespace std ;
using namespace std :: chrono ;
auto Get_Avg( int S[2] ){
return ( S[0] + S[1] )/2 ;
}
int lim = 0 ;
void set_lim( int size ){
if( size >= 12500 ) lim = 12500 ;
else lim = (size-1) ;
}
void check( int* arr, int n ){
for( int i = 0 ; i < n-1 ; i++ ){
if(arr[i] > arr[i+1]){
cout << "\n\nFalse\n\n" ;
break ;
}
// cout << arr[i] << " " ;
// if( i % 13 == 0 && i != 0 ) cout << endl ;
}
}
void swap( int* a, int* b ){
int s = *a ;
*a = *b ;
*b = s ;
}
int Serial_partition( int* x, int l, int h ){
int piv = x[h] ;
int i = l-1 ;
for( int j = l; j <= h ; j++ ){
if( x[j] < piv ){
i++ ;
swap( &x[i], &x[j] ) ;
}
}
swap( &x[i+1], &x[h] ) ;
return i+1 ;
}
void Serial_Quick_Sort( int* arr, int l, int h ){
if( l < h ){
int half = Serial_partition( arr, l, h ) ;
Serial_Quick_Sort( arr, l, half - 1 ) ;
Serial_Quick_Sort( arr, half+1, h ) ;
}
}
int OpenMP_thrds = 0 ;
void OpenMP_Quick_Sort( int* arr, int l, int h ){
OpenMP_thrds++ ;
if( (h - l)/lim > OpenMP_thrds ){
if( l < h ){
int half ;
half = Serial_partition( arr, l, h ) ;
#pragma omp task
OpenMP_Quick_Sort( arr, l, half-1 ) ;
#pragma omp task
OpenMP_Quick_Sort( arr, half+1, h ) ;
}
}
else Serial_Quick_Sort( arr, l, h ) ;
}
void* POSIX_QS( void* i) ;
int s, POSIX_thrds = 0 ;
class Data{
public:
int* arr ;
int l ;
int h ;
Data(){
arr = new int[s] ;
l = 0 ;
h = 0 ;
}
Data( int* a, int L, int H ){
arr = a ;
l = L ;
h = H ;
}
};
void POSIX_Quick_Sort( int* arr, int l, int h ){
s = h+1 ;
Data d( arr, l, h ) ;
pthread_t t ;
pthread_create( &t, NULL, POSIX_QS, &d ) ;
pthread_join( t, NULL ) ;
}
void* POSIX_QS( void* i ){
POSIX_thrds++ ;
Data* a = (Data*) i ;
int* arr = new int[s] ;
if( (a->h - a->l)/lim > POSIX_thrds ){
arr = a->arr ;
int L = a->l ;
int H = a->h ;
if( L < H ){
pthread_t t1, t2 ;
Data* x = new Data ;
Data* y = new Data ;
int half = Serial_partition(a->arr, L, H) ;
x -> arr = a -> arr ;
y -> arr = a -> arr ;
x->h = half - 1 ;
x->l = L ;
pthread_create( &t1, NULL, POSIX_QS, x ) ;
y->h = H ;
y->l = half + 1 ;
pthread_create( &t2, NULL, POSIX_QS, y ) ;
pthread_join(t1, NULL) ;
pthread_join(t2, NULL) ;
}
}
else Serial_Quick_Sort( a->arr, a->l, a->h ) ;
pthread_exit(NULL) ;
}
void Get_QuickSort_Times( int size, int QS_times[3], int* a ){
int* a1 = new int[size] ;
int* a2 = new int[size] ;
int* a3 = new int[size] ;
set_lim(size) ;
for( int i = 0 ; i < size ; i++ )
a1[i] = a2[i] = a3[i] = a[i] ;
auto start = high_resolution_clock :: now () ;
auto stop = high_resolution_clock :: now () ;
auto time = duration_cast<microseconds>( stop - start ) ;
start = high_resolution_clock :: now () ;
Serial_Quick_Sort(a1, 0, size-1) ;
stop = high_resolution_clock :: now () ;
time = duration_cast<microseconds>( stop - start ) ;
QS_times[0] = time.count() ;
check(a1, size) ;
start = high_resolution_clock :: now () ;
#pragma omp parallel
#pragma omp single
OpenMP_Quick_Sort(a2, 0, size-1) ;
stop = high_resolution_clock :: now () ;
time = duration_cast<microseconds>( stop - start ) ;
QS_times[1] = time.count() ;
check(a2, size) ;
start = high_resolution_clock :: now () ;
POSIX_Quick_Sort(a3, 0, size-1) ;
stop = high_resolution_clock :: now () ;
time = duration_cast<microseconds>( stop - start ) ;
check(a3, size) ;
//cout << endl << time.count() << " microseconds in POSIX" << endl ;
//cout << " POSIX Threads : " << POSIX_thrds << endl << endl;
QS_times[2] = time.count() ;
delete a1, a2, a3, a ;
}