-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_Sort.cpp
171 lines (95 loc) · 2.69 KB
/
Selection_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
struct Compare{
int val ;
int index ;
} ;
#pragma omp declare reduction( Minimum : struct Compare : omp_out = omp_in.val > omp_out.val ? omp_out : omp_in)\
initializer( omp_priv = omp_orig )
struct Array{
int* arr ;
int size ;
};
void Serial_Selection_Sort( int* arr, int size ){
int i, j, min ;
for( int i = 0 ; i < size-1 ; i++ ){
min = i ;
for( j = i+1 ; j < size ; j++ ){
if( arr[j] < arr[min] )
min = j ;
}
if( min != i )
swap( &arr[min], &arr[i] ) ;
}
}
void OpenMP_Selection_Sort( int* arr, int size ){
int i, j ;
#pragma omp single
for( int i = 0 ; i < size-1 ; i++ ){
struct Compare min ;
min.index = i ;
min.val = arr[i] ;
// #pragma omp parallel for reduction( Minimum : min )
for( j = i+1 ; j < size ; j++ ){
if( arr[j] < min.val ){
min.index = j ;
min.val = arr[j] ;
}
}
if( min.index != i )
swap( &arr[min.index], &arr[i] ) ;
}
}
void* POSIX_SS( void* x ){
struct Array* a = (Array*) x ;
int i, j, min ;
for( i = 0 ; i < a->size-1 ; i++ ){
min = i ;
for( j = i+1 ; j < a->size ; j++ ){
if( a->arr[j] < a->arr[min] )
min = j ;
}
if( min != i )
swap( &a->arr[min], &a->arr[i] ) ;
}
pthread_exit(NULL) ;
}
void POSIX_Selection_Sort( int* arr, int size ){
struct Array* a ;
pthread_t t ;
a->arr = arr ;
a->size = size ;
pthread_create( &t, NULL, POSIX_SS, (void*)a ) ;
pthread_join(t, NULL) ;
}
void Get_SelectionSort_Times(int size, int SS_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_Selection_Sort(a1, size) ;
stop = high_resolution_clock :: now () ;
time = duration_cast<microseconds>( stop - start ) ;
SS_times[0] = time.count() ;
check(a1, size) ;
start = high_resolution_clock :: now () ;
OpenMP_Selection_Sort(a2, size) ;
stop = high_resolution_clock :: now () ;
time = duration_cast<microseconds>( stop - start ) ;
SS_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;
SS_times[2] = time.count() ;
check(a3, size) ;
delete a1, a2, a3 ;
}