-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.cpp
29 lines (26 loc) · 863 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
#include "utility.h"
void quickSort(int* array, int left, int right, int size, bool verbose) {
//Step 1: Find pivot of array (naively use middle of array)
int pivotIndex = (left + right) / 2;
int pivotVal = array[pivotIndex];
//Step 2: Partition array
int i = left;
int j = right;
while(!(i > j)) {
while(array[i] < pivotVal)
i++;
while(array[j] > pivotVal)
j--;
if(i <= j) { //swap elements so that smaller is on left of pivot, larger on right
swap(array, i, j);
printArray(array, size, verbose);
i++; //advance i and j to next location
j--;
}
}
//Step 3: Recursively sort subarrays
if(left < j)
quickSort(array, left, j, size, verbose);
if(right > i)
quickSort(array, i, right, size, verbose);
}