-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
71 lines (52 loc) · 1.92 KB
/
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
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
63
64
65
66
67
68
69
70
71
#include <vector>
#include "QuickSort.h"
using namespace std;
// Problem: This particular implementation doesn't have a good partitioning scheme. See header for details.
bool QuickSort::Sort(vector<int>& v)
{
// Exit of less than 2 elements
if (v.size() < 2)
return false;
unsigned first = 0;
unsigned last = static_cast<unsigned>(v.size()) - 1;
return QSort(v, first, last);
}
bool QuickSort::QSort(vector<int>& v, unsigned first, unsigned last)
{
unsigned i, j;
int swap, median;
i = first;
j = last;
// get median element
median = v[(first + last) / 2];
// Inner loop - Scans for elements that are out of place with respect to the median (pivot) element and swaps them.
do {
// scan from the first towards the last until we find a value that is greater than the median.
while (v[i++] < median);
// The test occurred at i but then incremented to i + 1. Decrement i once so it's back to i for the comparison.
i--;
// scan form the last towards the first until we find a value that is less than the median.
while (median < v[j--]);
// The test occurred at j but then decremented to j - 1. Increment j once so it's back to for the comparison.
j++;
// Comparison - If the value greater than the median (i) is located to the
// left of the value that is less than the median (j) then they are out of order
// and need to be swapped.
if (i <= j) {
swap = v[i];
v[i++] = v[j];
v[j--] = swap;
}
// Continue scanning from where we left off until the i scan towards last element crosses the
// j scan towards the first element.
} while (i <= j && j != UINT_MAX);
// the i scan has now crossed the j scan.
// Recursively sort left side.
if (first < j && j != UINT_MAX)
QSort(v, first, j);
// Recursively sort right side.
if (i < last)
QSort(v, i, last);
// This section of the array is now sorted
return true;
}