Repository navigation
Expand file tree
/
Copy pathsorts.cpp
More file actions
218 lines (181 loc) · 5.29 KB
/
Copy pathsorts.cpp
File metadata and controls
218 lines (181 loc) · 5.29 KB
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
#include <iostream>
#include <random>
#include <time.h>
#include <algorithm>
#include <chrono>
using namespace std;
using namespace chrono;
// #define ARR_SIZE 1e10
const int ARR_SIZE = 2e5;
// const int ARR_SIZE = 10;
void min_max(int i,int j,double*arr,double*min,double*max);
void min_max_pos(int i,int j,double*arr,int*min,int*max);
void min_max_sort(double*arr);
void bubble_sort(double*arr);
void merge_sort(double*arr,int size);
void merge_sort(double*a,double*b,int left,int right);
void min_max(int i,int j,double*arr,double*min,double*max) {
int mid;
double lmin, lmax, rmin, rmax;
if (i == j) {
*min = *(arr+i);
*max = *(arr+j);
} else if (j == i + 1) {
if (*(arr+i)>*(arr+j)) {
*min = *(arr+j);
*max = *(arr+i);
} else {
*min = *(arr+i);
*max = *(arr+j);
}
} else {
mid = (i + j) / 2;
min_max(i, mid,arr, &lmin, &lmax);
min_max(mid + 1, j,arr, &rmin, &rmax);
*min = lmin<rmin ? lmin : rmin;
*max = lmax>rmax ? lmax : rmax;
}
}
void min_max_pos(int i,int j,double*arr,int*min,int*max) {
int mid;
int lmin, lmax, rmin, rmax;
if (i == j) {
*min = i;
*max = j;
} else if (j == i + 1) {
if (*(arr+i)>*(arr+j)) {
*min = j;
*max = i;
} else {
*min = i;
*max = j;
}
} else {
mid = (i + j) / 2;
min_max_pos(i, mid,arr, &lmin, &lmax);
min_max_pos(mid + 1, j,arr, &rmin, &rmax);
*min = *(arr+lmin)<*(arr+rmin) ? lmin : rmin;
*max = *(arr+lmax)>*(arr+rmax) ? lmax : rmax;
}
}
void min_max_sort(double*arr){
int midpt = ARR_SIZE/2 ;
int min,max;
int n = ARR_SIZE-1;
double temp;
for(int i = 0;i<midpt;i++){
min_max_pos(i,n-i,arr,&min,&max);
// if(min!=i){
temp = arr[i];
*(arr+i) = *(arr+min);
*(arr+min) = temp;
// }
// if(max!=n-i){
temp = arr[n-i];
*(arr+n-i) = *(arr+max);
*(arr+max) = temp;
// }
}
}
void bubble_sort(double*arr){
bool done = false;
double temp;
for(int k = 1;k<ARR_SIZE-1;k++){
done = true;
for(int i=0;i<ARR_SIZE-k;i++){
if(arr[i]>arr[i+1]){
temp = arr[i+1];
*(arr+i+1) = *(arr+i);
*(arr+i) = temp;
done = false;
}
}
if (done)break;
}
}
void merge_sort(double*arr,int size){
double*b = new double[size];
merge_sort(b,arr,0,size);
}
void merge_sort(double*a,double*b,int left,int right){
if(left+1==right)
return;
int mid = (left+right)/2;
merge_sort(b,a,left,mid);
merge_sort(b,a,mid,right);
for (int i=left,j=mid,pos=left; pos < right; ++pos)
{
if(j>=right || (i<mid && a[i]<a[j]))
b[pos]=a[i++];
else
b[pos]=a[j++];
}
}
int main(){
double*arr = new double[ARR_SIZE];
high_resolution_clock::time_point t1,t2;
t1 = high_resolution_clock::now();
{
const long double stddev =100;
random_device rd;
mt19937 e2(rd());
normal_distribution<double> dist(0,stddev);
double r;
for(int i = 0; i<ARR_SIZE;i++){
r = dist(e2);
*(arr+i) = r;
}
// double temp;
// for(int i=0;i<ARR_SIZE;i++){
// cin >> temp;
// arr[i] = temp;
// }
// cout << endl;
}
t2 = high_resolution_clock::now();
auto generation_duration = duration_cast<milliseconds>( t2 - t1 ).count();
cout << "Generatoin Duration : "<< generation_duration << endl;
cout << endl << endl;
// t1 = high_resolution_clock::now();
// {
// double min_max_arr[ARR_SIZE];
// for(int i=0;i<ARR_SIZE;i++){
// min_max_arr[i] = *(arr+i);
// }
// min_max_sort(&min_max_arr[0]);
// cout << endl << endl;
// }
// t2 = high_resolution_clock::now();
// auto min_max_sort_duration = duration_cast<milliseconds>( t2 - t1 ).count();
// cout << "Min-Max Sort Duration : "<< min_max_sort_duration << endl;
// cout << endl << endl;
// t1 = high_resolution_clock::now();
// {
// double bubble_arr[ARR_SIZE];
// for(int i=0;i<ARR_SIZE;i++){
// bubble_arr[i] = *(arr+i);
// }
// cout << endl;
// bubble_sort(&bubble_arr[0]);
// cout << endl << endl;
// }
// t2 = high_resolution_clock::now();
// auto bubble_sort_duration = duration_cast<milliseconds>( t2 - t1 ).count();
// cout << "Bubble Sort Duration : "<< bubble_sort_duration << endl;
// cout << endl << endl;
t1 = high_resolution_clock::now();
{
double merge_arr[ARR_SIZE];
for(int i=0;i<ARR_SIZE;i++){
merge_arr[i] = *(arr+i);
}
cout << endl;
merge_sort(&merge_arr[0],ARR_SIZE);
cout << endl << endl;
}
t2 = high_resolution_clock::now();
auto merge_sort_duration = duration_cast<milliseconds>( t2 - t1 ).count();
cout << "Merge Sort Duration : "<< merge_sort_duration << endl;
cout << endl << endl;
return 0;
}