-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
59 lines (50 loc) · 1.58 KB
/
QuickSort.cpp
File metadata and controls
59 lines (50 loc) · 1.58 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
#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;
const int N = 1e6;
double a[N+5];
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
/// QuickSort
void QuickSort(int l, int r) {
assert(l < r && 1 <= l && r <= N);
int p = l + rand() % (r - l);
assert(l <= p && p <= r);
swap(a[p], a[r]);
double pivot = a[r];
int i = l - 1;
for(int j = l; j < r; ++j){
if(a[j] <= pivot){
++i;
swap(a[i], a[j]);
}
}
++i;
swap(a[i], a[r]);
if(l < i-1)QuickSort(l, i-1);
if(i+1 < r)QuickSort(i+1, r);
}
signed main(){
srand(time(NULL));
ios::sync_with_stdio(false);
cin.tie(0);
freopen("QuickSortResult.out", "w", stdout);
for(int i = 1; i <= 10; ++i){
string filename = "TEST/test";
filename = filename + to_string(i) + ".inp";
const char* filename_cstr = filename.c_str();
freopen(filename_cstr, "r", stdin);
for(int j = 1; j <= N; ++j)cin >> a[j];
/// calculate time
auto start = chrono::high_resolution_clock::now();
QuickSort(1, N);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> elapsed = end - start;
/// check for sorted
for(int j = 1; j < N; ++j)if(a[j] > a[j+1]){
cout << fixed << setprecision(6) << j << ' ' << a[j] << ' ' << a[j+1] << endl;
return 0;
}
cout << "Test " << i << ": " << elapsed.count() << " ms" << endl;
}
}