-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
52 lines (47 loc) · 1.13 KB
/
Copy pathQuickSort.cpp
File metadata and controls
52 lines (47 loc) · 1.13 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
#include "include/QuickSort.h"
#include <iostream>
using namespace std;
void QuickSort::Sort(vector<int>& arr) {
if (arr.size() <= 0) {
std::cout << "List is empty\n";
return;
}
QSort(arr, 0, arr.size() - 1);
}
void QuickSort::QSort(vector<int>& arr, int low, int high) {
if (low < high) {
int part = Partition(arr, low, high);
if (part == -1)
return;
QSort (arr, low, part - 1);
QSort (arr, part + 1, high);
}
}
int QuickSort::Partition(vector<int>& arr, int first, int last) {
/* Get the pivot value */
int pivot = first;
int up = first;
int down = last;
while (up < down) {
/* move the first pointer to a value greater than pivot */
while (arr[up] <= arr[pivot]) {
up++;
}
/* Move the last pointer to a value less than pivot */
while (arr[down] > arr[pivot]) {
down--;
}
/* if the up and down have moved exchange and continue */
if (up < down) {
int temp = arr[up];
arr[up] = arr[down];
arr[down] = temp;
continue;
} else {
int temp = arr[pivot];
arr[pivot] = arr[down];
arr[down] = temp;
}
return down;
}
}