-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.cpp
55 lines (44 loc) · 1.54 KB
/
quick_sort.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
53
54
55
#include <vector>
#include <assert.h>
#include "utilities.h"
using namespace std;
/*****************************************************************************************
* quick sort
****************************************************************************************/
//pseudocode found on Wikipedia
// left is the index of the leftmost element of the subarray
// right is the index of the rightmost element of the subarray (inclusive)
// number of elements in subarray = right-left+1
int partition(vector <int> &array, int left, int right) {
int pivotIndex = left + (right-left) / 2;
int pivotValue = array[pivotIndex];
swap( array[pivotIndex], array[right]);
int storeIndex = left;
int i;
for(i = left; i < right; i++) {
if( array[i] <= pivotValue) {
swap (array[i], array[storeIndex]);
storeIndex = storeIndex + 1;
}
}
swap( array[storeIndex], array[right] ); // Move pivot to its final place
return storeIndex;
}
void quicksort(vector<int> &array, int i, int k) {
if (i < k) {
int p = partition(array, i, k);
quicksort(array, i, p - 1);
quicksort(array, p + 1, k);
}
}
void quick_sort(vector<int> &array) {
int i = 0;
int k = array.size() - 1;
quicksort(array, i, k);
//expensive, so comment out when checked
//assert(is_increasingly_sorted(array));
//print_array(array);
}
/*****************************************************************************************
* end
****************************************************************************************/