-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
44 lines (31 loc) · 892 Bytes
/
Copy pathquick_sort.cpp
File metadata and controls
44 lines (31 loc) · 892 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
vector<int> quickSort(vector<int> arr) {
if (arr.size() < 2) {
return arr;
} else {
int pivot = arr[0];
vector<int> less {};
vector<int> greater {};
for (int i = 1; i < arr.size(); i++) {
if (arr[i] <= pivot) {
less.push_back(arr[i]);
} else {
greater.push_back(arr[i]);
}
}
vector<int> sortedLess = quickSort(less);
vector<int> sortedGreater = quickSort(greater);
sortedLess.push_back(pivot);
sortedLess.insert(sortedLess.end(), sortedGreater.begin(), sortedGreater.end());
return sortedLess;
}
}
int main() {
vector<int> input {1, 10, 5, 6, 2, 12, 90};
for (int x : quickSort(input)) {
cout << x << " ";
}
return 0;
}