-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
52 lines (42 loc) · 1.21 KB
/
QuickSort.cpp
File metadata and controls
52 lines (42 loc) · 1.21 KB
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 <cstdio> // Provides printf()
#include <cstdlib> // Provides rand()
#include <vector> // Provides vector
#include <algorithm> // Provides swap()
using namespace std;
void quickSort(vector<int>& V, int left, int right);
int partition(vector<int>& V, int left, int right);
void print(const vector<int>& V);
int main() {
vector<int> V = {4,5,2,1,5,7,2,6};
quickSort(V, 0, V.size() - 1);
print(V);
return 0;
}
void quickSort(vector<int>& V, int left, int right) {
if (left >= right) return;
int pivotIndex = partition2(V, left, right);
quickSort(V, left, pivotIndex - 1);
quickSort(V, pivotIndex + 1, right);
}
int partition(vector<int>& V, int left, int right) {
// Choose pivot, swap with end. V[right] is pivot
int pivotIndex = (left + right) / 2;
swap(V[pivotIndex], V[right]);
// Reset pivotIndex to find new swaps
pivotIndex = left;
for (int i = left; i < right; ++i) {
if (V[i] <= V[right]) {
swap(V[i], V[pivotIndex]);
++pivotIndex;
}
}
// Swap pivot to correct place and return
swap(V[pivotIndex], V[right]);
return pivotIndex;
}
void print(const vector<int>& V) {
printf("[");
for (int i = 0; i < V.size() - 1; ++i)
printf("%i, ", V[i]);
printf("%i]\n", V[V.size() - 1]);
}