-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquickSort.cpp
49 lines (38 loc) · 1.17 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
#include <vector>
#include <iostream>
#include <utility>
// This partition simply picks the pivot to be the end of the vector
// This can be optimized by doing some sampling/picking middle/etc
int partition(std::vector<int> &vec, int left, int right) {
int pivot = --right;
while (true) {
while(vec[left] < vec[pivot])
++left;
while (left < right && vec[right - 1] >= vec[pivot])
--right;
if (left >= right) break;
std::swap(vec[left], vec[right - 1]);
}
std::swap(vec[left], vec[pivot]);
return left;
}
void quick_sort(std::vector<int> &vec, int left, int right) {
if (left + 1 >= right) return;
int pivot = partition(vec, left, right);
quick_sort(vec, left, pivot);
quick_sort(vec, pivot + 1, right);
}
int main() {
std::vector<int> vec1 = {5, 3, 10, 7, 30, 30, 23, 21 };
std::cout << "Before: " << std::endl;
for (int n : vec1) {
std::cout << n << " ";
}
std::cout << std::endl;
quick_sort(vec1, 0, vec1.size());
std::cout << "After quick_sort: " << std::endl;
for (int n : vec1) {
std::cout << n << " ";
}
std::cout << std::endl;
}