-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
63 lines (55 loc) · 1.68 KB
/
quicksort.cpp
File metadata and controls
63 lines (55 loc) · 1.68 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
53
54
55
56
57
58
59
60
61
62
// Muhammad Abed
// CSCI 3110 - 002
// Lab Sorting
// 11/11/15
// Implementation file for a quick sort
#include "quicksort.h"
#include "insertionSort.h"
// Function that implements a quick sort, kicks off the recursion
void quicksort (int array[], int size) {
clock_t begin, end;
begin = clock();
quicksortRec(array, 0, size-1);
end = clock();
std::cout << "\t" << diffClocks(end, begin);
}
// Recursively implements a quick sort
void quicksortRec (int array[], int first, int last) {
int pivotIndex;
if (first < last) { // base case
pivotIndex = partition(array, first, last);
quicksortRec(array, first, pivotIndex - 1);
quicksortRec(array, pivotIndex + 1, last);
}
}
// Breaks the array into partitions, based around the pivot
int partition (int array[], int first, int last) {
int pivotIndex;
int p;
int mid = first + (first - last)/2;
int i, j;
/*if ((array[first] <= array[mid] && array[mid] <= array[last]) || (array[last] <= array[mid] && array[mid] <= array[first]))
p = mid;
else if ((array[mid] <= array[first] && array[first] <= array[last]) || (array[last] <= array[first] && array[first] <= array[mid]))
p = first;
else if ((array[mid] <= array[last] && array[last] <= array[first]) || (array[first] <= array[last] && array[last] <= array[mid]))
p = last;
else*/
p = first;
pivotIndex = array[p];
if (p != first)
std::swap(array[p], array[first]);
// Loop through the array, comparing the current index to the pivot
i = first + 1;
j = last;
while (i <= j) {
while (i <= j && array[i] <= pivotIndex)
i++;
while (i <= j && array[j] > pivotIndex)
j--;
if (i < j)
std::swap(array[i], array[j]);
}
std::swap(array[i-1], array[first]);
return i-1;
}