-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathquickSort.h
41 lines (30 loc) · 865 Bytes
/
quickSort.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
#include <vector>
using namespace std;
// QUICKSORT
// -------------------------------------------------------------------------
// Partition function
int partition(vector <int> &v, int start, int end) {
int pivot = start; // Take the first element as a pivot
int i = start + 1;
int j = end;
while (i <= j) {
// If element at the left is bigger than the pivot and
// element at the right is smaller, swap elements
if (v[i] > v[pivot] && v[j] < v[pivot]) {
swap(v[i], v[j]);
}
else if (v[i] <= v[pivot]) { i++; }
else if (v[j] >= v[pivot]) { j--; }
}
// we swap the pivot into the right position
swap(v[j], v[pivot]);
return j;
}
// QuickSort
void quickSort(vector <int> &v, int start, int end) {
if (start < end) {
int pivot = partition(v, start, end);
quickSort(v, start, pivot);
quickSort(v, pivot + 1, end);
}
}