-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.cpp
More file actions
355 lines (327 loc) · 10.5 KB
/
Sort.cpp
File metadata and controls
355 lines (327 loc) · 10.5 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Sort.cpp
*
* Sorting project
* by Nishika Pabba 8/6/2021
*
* Implementation of sorting class. Uses quickSort and binary search algorithms.
*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include "Sort.h"
#include "Person.h"
/* Sort
* Purpose: Constructor
* Parameters: String of file name with persons list.
* Returns: NONE
*/
Sort::Sort(string personsFile){
readFile(personsFile);
}
/* readFile
* Purpose: To open file and collect names and info of each person.
* Parameters: String of file name with persons list.
* Returns: NONE
*/
void Sort::readFile(string personsFile){
ifstream infile(personsFile);
if(infile.fail()){
cerr << "ERROR: " << personsFile << " is an invalid file." << endl;
exit(EXIT_FAILURE);
}
string personInfo = "";
string first, last, trash;
int hourTime, minTime;
getline (infile, personInfo);
istringstream ss(personInfo);
ss >> first >> last >> hourTime >> minTime;
Person newPerson = Person(first, last, hourTime, minTime);
list.push_back(newPerson);
while(!infile.eof()){
getline(infile, personInfo);
istringstream sstream(personInfo);
sstream >> first >> last >> hourTime >> minTime;
Person newPerson = Person(first, last, hourTime, minTime);
list.push_back(newPerson);
}
list.pop_back();
}
/* print
* Purpose: To print people's info from list.
* Parameters: ostream reference for output file.
* Returns: NONE
*/
void Sort::print(ostream &output){
output << endl << "--------------------------" << endl;
output << "--------------------------" << endl << endl;
for(int i = 0; i < list.size(); i++){
try {
list[i].print(output);
} catch (runtime_error &message){
output << message.what() << endl;
output << endl;
}
}
output << "--------------------------" << endl;
output << "--------------------------" << endl << endl;
}
/* organize
* Purpose: To pass in output file reference depending on command line
* arguments given by the user.
* Parameters: string of output file name
* Returns: NONE
*/
void Sort::organize(string outputFile){
if(outputFile == "") {
query(cout);
} else {
ofstream out(outputFile);
if (out.fail()){
cerr << "ERROR: " << outputFile << " is an invalid file." << endl;
exit(EXIT_FAILURE);
}
query(out);
}
}
/* initialMessage
* Purpose: To print message for user input.
* Parameters: ostream reference for output file.
* Returns: NONE
*/
void Sort::initialMessage(ostream &outputFile){
outputFile << "Which of the following tasks would you like to complete?"
<< endl;
outputFile << "Type the number from one of the following options:" << endl;
outputFile << "1. alphabetical by first name" << endl;
outputFile << "2. alphabetical by last name" << endl;
outputFile << "3. chronological" << endl;
outputFile << "4. search for person by first name" << endl;
outputFile << "5. search for person by last name" << endl;
}
/* query
* Purpose: Handles user query inputs from terminal.
* Parameters: ostream reference for output file.
* Returns: NONE
*/
void Sort::query(ostream &outputFile){
string command;
//prints initial unsorted info per person
print(outputFile);
//prints introductory message
initialMessage(outputFile);
//query loop
while (cin >> command){
if(command == "1"){
int num = list.size()-1;
quickSortFirst(0, num);
print(outputFile);
} else if (command == "2"){
int num = list.size()-1;
quickSortLast(0, num);
print(outputFile);
} else if (command == "3"){
int num = list.size()-1;
quickSortTime(0, num);
print(outputFile);
} else if (command == "4"){
string name;
outputFile << "Enter first name to search for:" << endl;
cin >> name;
int num = list.size()-1;
foundIndex = -1;
binarySearchFirst(0, num, name);
if (foundIndex > -1) {
outputFile << endl << "Found the person at index ["
<< foundIndex << "]!" << endl;
outputFile << endl
<< "*** list was sorted alphabetically by first name ***"
<< endl << endl;
} else {
outputFile << "Person not found :(" << endl;
}
} else if (command == "5"){
string name;
bool found;
outputFile << "Enter last name to search for:" << endl;
cin >> name;
int num = list.size()-1;
foundIndex = -1;
binarySearchLast(0, num, name);
if (foundIndex > -1) {
outputFile << endl << "Found the person at index ["
<< foundIndex << "]!" << endl;
outputFile << endl
<< "*** list was sorted alphabetically by last name ***"
<< endl << endl;
} else {
outputFile << "Person not found :(" << endl;
}
} else {
outputFile << endl << "COMMAND NOT FOUND. Please try again."
<< endl << endl;
}
//prints initial message screen so that loop can continue
initialMessage(outputFile);
}
}
/* comparison
* Purpose: Compares letters between two strings.
* Parameters: ostream reference for output file.
* Returns: NONE
*/
bool Sort::comparison(string a, string b){
//if letters are equal, iterate to next letter until differing
//letter is found
for(int i = 0; i < a.length(); i++){
//compare differing letters
if(a[i] < b[i]){
return true;
} else if (a[i] > b[i]){
return false;
}
}
return false;
}
/* partitionFirst
* Purpose: Chooses pivot point and partitions list by first name to sort
* recursively.
* Parameters: Integers of small and big indices for the range of partition.
* Returns: Next integer for swapping.
*/
int Sort::partitionFirst(int small, int big){
string pivot = list[big].firstName;
int j = small-1;
for (int i = small; i <= big; i++){
if (comparison(list[i].firstName, pivot)) {
j++;
swap(list[j], list[i]);
}
}
swap(list[j+1], list[big]);
return j+1;
}
/* quickSortFirst
* Purpose: Uses quickSort algorithm to partition and sort first names.
* Calls partition function to recursively create partitions.
* Parameters: Integers to signify the indices of the range of partition.
* Returns: NONE
*/
void Sort::quickSortFirst(int small, int big){
if (small < big){
int index = partitionFirst(small, big);
quickSortFirst(small, index-1);
quickSortFirst(index+1, big);
}
}
/* partitionLast
* Purpose: Chooses pivot point and partitions list by last name to sort
* recursively.
* Parameters: Integers of small and big indices for the range of partition.
* Returns: Next integer for swapping.
*/
int Sort::partitionLast(int small, int big){
string pivot = list[big].lastName;
int j = small-1;
for (int i = small; i <= big; i++){
if (comparison(list[i].lastName, pivot)) {
j++;
swap(list[j], list[i]);
}
}
swap(list[j+1], list[big]);
return j+1;
}
/* quickSortLast
* Purpose: Uses quickSort algorithm to partition and sort last names.
* Calls partition function to recursively create partitions.
* Parameters: Integers to signify the indices of the range of partition.
* Returns: NONE
*/
void Sort::quickSortLast(int small, int big){
if (small < big){
int index = partitionLast(small, big);
quickSortLast(small, index-1);
quickSortLast(index+1, big);
}
}
/* partitionTime
* Purpose: Chooses pivot point and partitions list by time to sort
* recursively.
* Parameters: Integers of small and big indices for the range of partition.
* Returns: Next integer for swapping.
*/
int Sort::partitionTime(int small, int big){
int pivot = list[big].totalMin;
int j = small-1;
for (int i = small; i <= big; i++){
if (list[i].totalMin < pivot) {
j++;
swap(list[j], list[i]);
}
}
swap(list[j+1], list[big]);
return j+1;
}
/* quickSortTime
* Purpose: Uses quickSort algorithm to partition and sort time.
* Calls partition function to recursively create partitions.
* Parameters: Integers to signify the indices of the range of partition.
* Returns: NONE
*/
void Sort::quickSortTime(int small, int big){
if (small < big){
int index = partitionTime(small, big);
quickSortTime(small, index-1);
quickSortTime(index+1, big);
}
}
/* binarySearchFirst
* Purpose: Sorts first names alphabetically and then uses binary search
* algorithm to determine if a first name is in the list.
* Parameters: Integers to signify the indices of the range of partition.
* Returns: Boolean about whether the name was found or not.
*/
void Sort::binarySearchFirst(int start, int end, string name){
quickSortFirst(start, end);
int mid = (start + end) / 2;
while (start <= end) {
if (list[mid].firstName == name ){
foundIndex = mid;
return;
}
if(comparison(list[mid].firstName, name)){
binarySearchFirst(mid + 1, end, name);
} else {
binarySearchFirst(start, mid - 1, name);
}
return;
}
}
/* binarySearchLast
* Purpose: Sorts last names alphabetically and then uses binary search
* algorithm to determine if a last name is in the list.
* Parameters: Integers to signify the indices of the range of partition.
* Returns: Boolean about whether the name was found or not.
*/
void Sort::binarySearchLast(int start, int end, string name){
quickSortLast(start, end);
int mid = (start + end)/ 2;
while(start <= end){
if (list[mid].lastName == name ){
foundIndex = mid;
return;
}
if(comparison(list[mid].lastName, name)){
binarySearchLast(mid + 1, end, name);
} else {
binarySearchLast(start, mid - 1, name);
}
return;
}
}