-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-sort.cpp
56 lines (40 loc) Β· 1.5 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
// cl /W4 /EHsc quick-sort.cpp /Fo.\output\ /Fe.\output\ /link && .\output\quick-sort.exe
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
void main() {
auto swap = [](vector<int>& array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
};
function<void(vector<int>&, int, int)> quickSort;
function<int(vector<int>&, int, int)> partition;
quickSort = [&quickSort, &partition](vector<int>& array, int p, int r) {
if (p < r) {
int q = partition(array, p, r);
quickSort(array, p, q - 1);
quickSort(array, q + 1, r);
}
};
partition = [&swap](vector<int>& array, int p, int r) {
int q = p;
for (int j = p; j <= r - 1; j++) {
if (array[j] <= array[r]) {
swap(array, j, q);
q++;
}
}
swap(array, r, q);
return q;
};
vector<int> array = { 3, 7, 12, 14, 2, 6, 9, 11 };
cout << "array: "; for (auto i : array) { cout << i << ", "; } cout << endl;
quickSort(array, 0, (int)array.size() - 1);
cout << "sorted: "; for (auto i : array) { cout << i << ", "; } cout << endl;
array = { 99, 97, -3, -55, -6, 7, 8, 14, 31, 32, -0, 0, 3, 2 };
cout << "array: "; for (auto i : array) { cout << i << ", "; } cout << endl;
quickSort(array, 0, (int)array.size() - 1);
cout << "sorted: "; for (auto i : array) { cout << i << ", "; } cout << endl;
};