-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
306 lines (270 loc) · 10.1 KB
/
Copy pathQuickSort.cpp
File metadata and controls
306 lines (270 loc) · 10.1 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "QuickSort.h"
//Citation: Modified version of quickSort algorithm written by: Amanpreet Kapoor
QuickSort::QuickSort() = default;
//Read in Data from csv
void QuickSort::readInData() {
string token;
ifstream AirportFile("Proj3_Airlines.csv");
if (AirportFile.is_open()) {
//clear data's headers
string currentLine;
getline(AirportFile, currentLine);
int index = 0;
//store necessary data only while reading over other values
while (getline(AirportFile, currentLine)){
Airport newAp;
istringstream stream(currentLine);
for (int i = 0; i < 9; i++) {
getline(stream, token, ',');
if (i == 0) { newAp.setCode(token); }
else if (i == 1) {newAp.setName(token);}
else if (i == 2) {newAp.setNumLate(stoi(token)); }
else if (i == 3) {newAp.setSecurityDelays(stoi(token));}
else if (i == 4) {newAp.setNumWeatherDelays(stoi(token));}
else if (i == 5) {newAp.setNumFlightsCancelled(stoi(token));}
else if (i == 6) {newAp.setNumDelayedFlights(stoi(token));}
else if (i == 7) {newAp.setNumFlightsTotaled(stoi(token));}
else { newAp.setNumMinutesDelayed(stoi(token));}
}
airports[index] = newAp;
index++;
}
}
else{
cout << "still does not load file" << endl;
}
}
//quick sort values depending on subject
void QuickSort::quickSort(int low, int high, string& dataType) {
int pivot;
if (low < high){
//checks for subject and calls appropriate partition call
if(dataType == "Late"){pivot = partitionLateFlights(low, high);}
else if(dataType == "Security"){pivot = partitionSecurityDelays(low, high);}
else if(dataType == "Weather"){pivot = partitionWeatherDelays(low,high);}
else if(dataType == "Cancelled"){pivot = partitionFlightsCancelled(low,high);}
else if(dataType == "Delayed"){pivot = partitionDelayedFlights(low,high);}
else if(dataType == "Total"){pivot = partitionFlightsTotal(low,high);}
else{pivot = partitionMinutesDelayed(low,high);}
quickSort(low, pivot - 1, dataType);
quickSort(pivot + 1, high, dataType);
}
}
//partitions based on # of late flights in each airport object
int QuickSort::partitionLateFlights(int low, int high) {
int pivot = airports[low].getNumLate();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumLate() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumLate() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partitions based on # of security delays in airport object
int QuickSort::partitionSecurityDelays(int low, int high) {
int pivot = airports[low].getSecurityDelays();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getSecurityDelays() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getSecurityDelays() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partitions based on # of weather delays in each airport object
int QuickSort::partitionWeatherDelays(int low, int high) {
int pivot = airports[low].getNumWeatherDelays();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumWeatherDelays() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumWeatherDelays() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partition based on number of flights cancelled in each airport object
int QuickSort::partitionFlightsCancelled(int low, int high) {
int pivot = airports[low].getNumFlightsCancelled();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumFlightsCancelled() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumFlightsCancelled() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partitions based on number of delayed flights in each airport object
int QuickSort::partitionDelayedFlights(int low, int high) {
int pivot = airports[low].getNumDelayedFlights();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumDelayedFlights() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumDelayedFlights() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partitions based on number of flights total in each airport object
int QuickSort::partitionFlightsTotal(int low, int high) {
int pivot = airports[low].getNumFlightsTotaled();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumFlightsTotaled() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumFlightsTotaled() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//partitions based on number of total minutes delayed for each airport object
int QuickSort::partitionMinutesDelayed(int low, int high) {
int pivot = airports[low].getNumMinutesDelayed();
int up = low, down = high;
while (up < down) {
for (int j = up; j < high; j++) {
if (airports[up].getNumMinutesDelayed() > pivot)
break;
up++;
}
for (int j = high; j > low; j--) {
if (airports[down].getNumMinutesDelayed() < pivot)
break;
down--;
}
if (up < down)
std::swap(airports[low], airports[down]);
}
std::swap(airports[low], airports[high]);
return down;
}
//helper function for calling quicksort
void QuickSort::callQuickSort(string& desiredValue) {
int n = sizeof(airports) / sizeof(airports[0]);
quickSort(0, n-1, desiredValue);
}
//classifies desired value and prints appropriately
void QuickSort::callPrint(string &dataType) {
if(dataType == "Late"){
printLate();
}
else if(dataType == "Security"){
printSecurity();
}
else if(dataType == "Weather"){
printWeatherDelay();
}
else if(dataType == "Cancelled"){
printFlightsCancelled();
}
else if(dataType == "Delayed"){
printDelayedFlights();
}
else if(dataType == "Total"){
printFlightsTotal();
}
else{
printMinutesDelayed();
}
}
void QuickSort::printLate() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Late Flights: " << airports[i].getNumLate() << endl;
}
}
void QuickSort::printSecurity() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Security Delays: " << airports[i].getSecurityDelays() << endl;
}
}
void QuickSort::printWeatherDelay() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Weather Delays: " << airports[i].getNumWeatherDelays() << endl;
}
}
void QuickSort::printFlightsCancelled() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Flights Cancelled: " << airports[i].getNumFlightsCancelled() << endl;
}
}
void QuickSort::printDelayedFlights() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Flights Delayed: " << airports[i].getNumDelayedFlights() << endl;
}
}
void QuickSort::printFlightsTotal() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "Number of Flights Total: " << airports[i].getNumFlightsTotaled() << endl;
}
}
void QuickSort::printMinutesDelayed() {
for(int i = 0; i < (sizeof(airports) / sizeof(airports[0])); i++){
cout << (i+1) << " Airport Code and Name: " << airports[i].getAirportCode() + " " << airports[i].getAirportName() <<endl;
cout << "The Total Minutes Delayed: : " << airports[i].getNumMinutesDelayed()<< endl;
}
}