forked from BYUCS235/Quicksort-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQS.cpp
178 lines (151 loc) · 3.24 KB
/
QS.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "QS.h"
#include <sstream>
#include <string>
QS::QS(){}
QS::~QS(){
delete [] arrPtr;
}
bool QS::createArray(int cap){
elements = -1;
if(capacity < 0){
return false;
}
else{
if(elements > 0){
delete [] arrPtr;
}
capacity = cap;
arrPtr = new int[capacity];
elements = 0;
return true;
}
}
bool QS::addToArray(int value){
if(elements == capacity){
return false;
}
else{
arrPtr[elements] = value;
elements++;
return true;
}
}
string QS::getArray() const{
stringstream ss;
if(elements <= 0){
ss << "";
}
else{
for(int i = 0; i < elements; i++){
if(i == elements - 1){
ss << arrPtr[i];
}
else{
ss << arrPtr[i] << ",";
}
}
}
return ss.str();
}
int QS::getSize() const{
return elements;
}
void QS::clear(){
elements = 0;
}
void QS::sortAll(){
quickSort(0, elements - 1);
}
void QS::quickSort(int first, int last){
if(last - first < 1){
return;
}
int pivot = medianOfThree(first, last);
pivot = partition(first, last, pivot);
quickSort(first, pivot - 1);
quickSort(pivot + 1, last);
}
int QS::medianOfThree(int left, int right){
if(elements == 0){
return -1;
}
if(left < 0){
return -1;
}
if(right < 0){
return -1;
}
if(left > elements - 1){
return -1;
}
if(right > elements - 1){
return -1;
}
if(right <= left){
return -1;
}
int placeholder;
int middle = (left + right) / 2;
if(arrPtr[left] > arrPtr[middle]){
placeholder = arrPtr[left];
arrPtr[left] = arrPtr[middle];
arrPtr[middle] = placeholder;
}
if(arrPtr[middle] > arrPtr[right]){
placeholder = arrPtr[middle];
arrPtr[middle] = arrPtr[right];
arrPtr[right] = placeholder;
}
if(arrPtr[left] > arrPtr[middle]){
placeholder = arrPtr[left];
arrPtr[left] = arrPtr[middle];
arrPtr[middle] = placeholder;
}
return middle;
}
int QS::partition(int left, int right, int pivotIndex){
if(elements == 0){
return -1;
}
if(left < 0){
return -1;
}
if(right < 0){
return -1;
}
if(left > elements - 1){
return -1;
}
if(right > elements - 1){
return -1;
}
if(left >= right){
return -1;
}
if(pivotIndex < left || pivotIndex > right){
return -1;
}
int placeholder;
placeholder = arrPtr[pivotIndex];
arrPtr[pivotIndex] = arrPtr[left];
arrPtr[left] = placeholder;
int up = left + 1;
int down = right;
do{
while((arrPtr[up] <= arrPtr[left]) && (up < right)){
up++;
}
while((arrPtr[down] > arrPtr[left]) && (down > left)){
down--;
}
if(up < down){
placeholder = arrPtr[up];
arrPtr[up] = arrPtr[down];
arrPtr[down] = placeholder;
}
} while(up < down);
placeholder = arrPtr[left];
arrPtr[left] = arrPtr[down];
arrPtr[down] = placeholder;
return down;
}