-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cpp
52 lines (43 loc) · 966 Bytes
/
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
#include <vector>
#include <iostream>
#include <algorithm>
int Partition(std::vector<int>& items, int left, int right)
{
int x = items[right];
int less = left;
for(int i = left; i < right; ++i)
{
if(items[i] <= x)
{
std::swap(items[i], items[less]);
++less;
}
}
std::swap(items[less], items[right]);
return less;
}
void QuickSortImpl(std::vector<int>& items, int left, int right)
{
if(left < right)
{
int q = Partition(items, left, right);
QuickSortImpl(items, left, q - 1);
QuickSortImpl(items, q + 1, right);
}
}
void QuickSort(std::vector<int>& items)
{
if(items.empty() == false)
{
QuickSortImpl(items, 0, items.size() - 1);
}
}
int main()
{
std::vector<int> items {10, 25, 13, 6, 52, 1};
QuickSort(items);
for(const auto& item: items)
std::cout << item << ",";
std::cout << "\n";
return 0;
}