-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick.cpp
More file actions
63 lines (50 loc) · 1.28 KB
/
quick.cpp
File metadata and controls
63 lines (50 loc) · 1.28 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
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <cstdio>
using namespace std;
using namespace std::chrono;
int split( int*arr, int low, int high ) {
int i = low-1;
int temp;
for( int j = low; j < high; j++ )
if( arr[j] <= arr[high] ) {
i++;
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
i++;
temp = arr[high];
arr[high] = arr[i];
arr[i] = temp;
return i;
}
void quick_sort( int* arr, int low, int high ) {
if( low < high ) {
int pivot = split( arr, low, high );
quick_sort( arr, low, pivot-1 );
quick_sort( arr, pivot+1, high );
}
}
int main() {
for( int n = 1; n <= 1000000; n*=10 ) {
int num[n];
for( int i = 0; i < n; i++ )
//num[i] = rand()%n;
num[i] = i;
/* for( int i = 0; i < n; i++ )
cout << num[i] << "\t";
cout << endl; */
auto start = high_resolution_clock::now();
quick_sort( num, 0, n-1 );
auto stop = high_resolution_clock::now();
/* for( int i = 0; i < n; i++ )
cout << num[i] << "\t";
cout << endl; */
auto duration = duration_cast<microseconds>(stop - start);
cout << n << "\t:\t" << duration.count();
getchar();
}
return 0;
}