-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
42 lines (37 loc) · 1.14 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
#include <iostream>
#include <vector>
template <typename IteratorType,
typename ValueType,
typename Comparator = std::less<ValueType>
>
IteratorType Partition(IteratorType begin, IteratorType end,
ValueType value, Comparator comparator = Comparator()) {
while (begin < end) {
if (comparator(*begin, value)) {
++begin;
} else if (!comparator(*(end - 1), value)) {
--end;
} else {
std::iter_swap(begin++, --end);
}
}
return begin;
}
template <typename IteratorType,
typename Comparator = std::less<typename IteratorType::value_type>
>
void QuickSort(IteratorType begin, IteratorType end,
Comparator comparator = Comparator()) {
if (std::distance(begin, end) < 2) {
return;
}
auto pivot_iter = begin;
std::advance(pivot_iter, std::rand() % std::distance(begin, end));
auto pivot = *pivot_iter;
auto middle = Partition(begin, end, pivot, comparator);
QuickSort(begin, middle);
while (middle != end && *middle == pivot) {
++middle;
}
QuickSort(middle, end);
}