-
Notifications
You must be signed in to change notification settings - Fork 1
/
quick_sort.h
46 lines (36 loc) · 1.01 KB
/
quick_sort.h
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
#ifndef SORT_QUICK_SORT_H_
#define SORT_QUICK_SORT_H_
#include <vector>
using namespace std;
inline vector<int>::iterator partition(
const vector<int>::iterator begin, const vector<int>::iterator end) {
const auto pivot = begin;
auto bound = pivot;
auto it = pivot + 1;
while (it < end) {
if (*it < *pivot) {
++bound;
std::iter_swap(bound, it);
}
++it;
}
++it;
std::iter_swap(bound, pivot);
return bound;
}
inline void quickSortRecursive(
const vector<int>::iterator begin, const vector<int>::iterator end) {
if (end - begin <= 1) { return; }
const auto pivot = partition(begin, end);
quickSortRecursive(begin, pivot);
quickSortRecursive(pivot + 1, end);
}
// Sort by recursively partitioning array into elements smaller and bigger than
// pivot.
// T~O(N*log(N)), S~O(log(N))
inline void quickSort(vector<int>* data) {
if (data->empty()) { return; }
if (data->size() == 1) { return; }
quickSortRecursive(data->begin(), data->end());
}
#endif // SORT_QUICK_SORT_H_