-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3way-quick-sort.cpp
58 lines (50 loc) · 954 Bytes
/
3way-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
57
58
#include <iostream>
#include <vector>
#include <cstdlib>
using std::vector;
using std::swap;
void partition(vector<int> &a, int l, int r, int &i, int &j) {
if (r - l <= 1){
if (a[r] < a[l])
swap (a[r], a[l]);
i = l;
j = r;
return;
}
int m = l;
int pivot = a[r];
while (m <= r) {
if (a[m] < pivot)
swap(a[l++], a[m++]);
else if (a[m] == pivot)
m++;
else {
swap (a[r--], a[m]);
}
i = l-1;
j = m;
}
}
void quickSort(vector<int> &a, int l, int r) {
if (l >= r) {
return;
}
int k = l +(r - l)/2;
int i, j;
partition(a, l, r, i, j);
quickSort(a, l, i);
quickSort(a, j, r);
}
int main() {
int n;
std::cin >> n;
vector<int> a(n);
for (size_t i = 0; i < a.size(); ++i) {
std::cin >> a[i];
}
//randomized_quick_sort(a, 0, a.size() - 1);
quickSort(a, 0, a.size()-1);
for (size_t i = 0; i < a.size(); ++i) {
std::cout << a[i] << ' ';
}
}