-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
156 lines (138 loc) · 3.66 KB
/
quicksort.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
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
#include <bits/stdc++.h>
#include <cassert>
#include <functional>
#include <vector>
using namespace std;
int choose_pivot_3middle (vector<int> &arr, int lo, int hi) {
int mid = (lo + hi) >> 1;
if ( (arr[mid] - arr[lo]) * (arr[mid] - arr[hi]) <= 0 ) {
return mid;
} else if ( (arr[lo] - arr[mid]) * (arr[lo] - arr[hi]) <= 0) {
return lo;
} else {
return hi;
}
}
int lomuto_partition (vector<int> &arr, int lo, int hi) {
assert(lo <= hi);
if (lo == hi) {
return lo;
}
// invented by Nico Lomuto
// * pivot is the last one
// * like quick-slow pointers
int pivot_pos = choose_pivot_3middle(arr, lo, hi);
if (pivot_pos != hi) {
swap(arr[pivot_pos], arr[hi]);
}
int pivot = arr[hi];
int i = lo - 1;
for (int j = lo; j < hi; j++) {
if (arr[j] <= pivot) {
i += 1;
swap(arr[i], arr[j]);
}
}
i += 1;
swap(arr[hi], arr[i]);
return i;
// arr[lo, i] <= pivot, arr[i+1, hi] > pivot, arr[i] == pivot
}
int hoare_partition (vector<int> &arr, int lo, int hi) {
assert(lo <= hi);
if (lo == hi) {
return lo;
}
// invented by the Tony Hoare
// * pivot is the middle element
int pivot = arr[(lo + hi) >> 1];
int i = lo - 1, j = hi + 1;
while (true) {
do {
i ++;
}
while (arr[i] < pivot);
do {
j --;
}
while (arr[j] > pivot);
assert(i-j < 2);
if (i == j || i == j+1) {
return j;
}
swap(arr[i], arr[j]);
}
// arr[lo, j] <= pivot, arr[j+1, hi] >= pivot
}
int normal_partition (vector<int> &arr, int lo, int hi) {
assert(lo <= hi);
if (lo == hi) {
return lo;
}
// the first way I learnt
int pivot_pos = choose_pivot_3middle(arr, lo, hi);
if (pivot_pos != lo) {
swap(arr[pivot_pos], arr[lo]);
}
int pivot = arr[lo];
int i = lo, j = hi;
while (i < j) {
// arr[.., i) <= pivot, arr(j, ..] >= pivot
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] <= pivot) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
// i == j when jump out
assert(i == j);
arr[i] = pivot;
return i;
}
function<int(vector<int>&, int, int)> partition_wrapper;
void quicksort (vector<int> &arr, int lo, int hi) {
assert(lo <= hi);
if (lo == hi) {
return;
}
auto *partition_ptr = partition_wrapper.target<int(*)(vector<int>&, int, int)>();
assert(partition_ptr);
int mid = partition_wrapper(arr, lo, hi);
if (*partition_ptr == &lomuto_partition || *partition_ptr == &normal_partition) {
if (lo < mid) quicksort(arr, lo, mid - 1);
if (mid < hi) quicksort(arr, mid + 1, hi);
} else if (*partition_ptr == &hoare_partition) {
quicksort(arr, lo, mid);
quicksort(arr, mid + 1, hi);
}
}
int main (int argc, char *argv[]) {
if (argc != 2) {
cout << "need 0, 1, 2 to indicate which partition function" << endl;
return 1;
}
if (argv[1][0] == '0') {
partition_wrapper = lomuto_partition;
} else if (argv[1][0] == '1') {
partition_wrapper = hoare_partition;
} else if (argv[1][0] == '2') {
partition_wrapper = normal_partition;
}
// vector<int> arr {8, 6, 5, 3, 1};
// vector<int> arr {3, 4, 2, 1};
vector<int> arr {2, 3, 1};
quicksort(arr, 0, arr.size() - 1);
for (int x : arr) {
cout << x << ' ';
}
cout << endl;
}