-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorSortingAssignment.cpp
More file actions
318 lines (270 loc) · 9.34 KB
/
VectorSortingAssignment.cpp
File metadata and controls
318 lines (270 loc) · 9.34 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
//============================================================================
// Name : VectorSorting.cpp
// Author : Garret Gherardini
// Version : 1.0
// Copyright : Copyright © 2017 SNHU COCE
// Description : Vector Sorting Algorithms
//============================================================================
#include <algorithm>
#include <iostream>
#include <time.h>
#include "CSVparser.hpp"
using namespace std;
//============================================================================
// Global definitions visible to all methods and classes
//============================================================================
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};
//============================================================================
// Static methods used for testing
//============================================================================
/**
* Display the bid information to the console (std::out)
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount << " | "
<< bid.fund << endl;
return;
}
/**
* Prompt user for bid information using console (std::in)
*
* @return Bid struct containing the bid info
*/
Bid getBid() {
Bid bid;
cout << "Enter Id: ";
cin.ignore();
getline(cin, bid.bidId);
cout << "Enter title: ";
getline(cin, bid.title);
cout << "Enter fund: ";
cin >> bid.fund;
cout << "Enter amount: ";
cin.ignore();
string strAmount;
getline(cin, strAmount);
bid.amount = strToDouble(strAmount, '$');
return bid;
}
/**
* Load a CSV file containing bids into a container
*
* @param csvPath the path to the CSV file to load
* @return a container holding all the bids read
*/
vector<Bid> loadBids(string csvPath) {
cout << "Loading CSV file " << csvPath << endl;
// Define a vector data structure to hold a collection of bids.
vector<Bid> bids;
// initialize the CSV Parser using the given path
csv::Parser file = csv::Parser(csvPath);
try {
// loop to read rows of a CSV file
for (int i = 0; i < file.rowCount(); i++) {
// Create a data structure and add to the collection of bids
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');
//cout << "Item: " << bid.title << ", Fund: " << bid.fund << ", Amount: " << bid.amount << endl;
// push this bid to the end
bids.push_back(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
return bids;
}
// FIXME (2a): Implement the quick sort logic over bid.title
/**
* Partition the vector of bids into two parts, low and high
*
* @param bids Address of the vector<Bid> instance to be partitioned
* @param begin Beginning index to partition
* @param end Ending index to partition
*/
int partition(vector<Bid>& bids, int begin, int end) {
//set low and high equal to begin and end
int l = begin;
int h = end;
// pick the middle element as pivot point
int pivot = (begin + (end - begin) / 2);
// while not done
bool finished = false;
// keep incrementing low index while bids[low] < bids[pivot]
while (!finished) {
while (bids.[l].title.compare(bids[pivot].title) < 0) {
++l;
}
// keep decrementing high index while bids[pivot] < bids[high]
while (bids.[pivot].title.compare(bids[h].title) < 0) {
--h;
}
/* If there are zero or one elements remaining,
all bids are partitioned. Return high */
// else swap the low and high bids (built in vector method)
// move low and high closer ++low, --high
//return high;
if (l > h) {
finished = true;
}
else {
swap(bids[l], bids[h]);
++l;
--h;
}
}
return h;
}
/**
* Perform a quick sort on bid title
* Average performance: O(n log(n))
* Worst case performance O(n^2))
*
* @param bids address of the vector<Bid> instance to be sorted
* @param begin the beginning index to sort on
* @param end the ending index to sort on
*/
void quickSort(vector<Bid>& bids, int begin, int end) {
//set mid equal to 0
unsigned int middle = 0;
/* Base case: If there are 1 or zero bids to sort,
partition is already sorted otherwise if begin is greater
than or equal to end then return*/
if (begin >= end) {
return;
}
/* Partition bids into low and high such that
midpoint is location of last element in low */
middle = partition(bids, begin, end);
// recursively sort low partition (begin to mid)
quickSort(bids, begin, middle);
// recursively sort high partition (mid+1 to end)
quickSort(bids, middle + 1, end);
}
// FIXME (1a): Implement the selection sort logic over bid.title
/**
* Perform a selection sort on bid title
* Average performance: O(n^2))
* Worst case performance O(n^2))
*
* @param bid address of the vector<Bid>
* instance to be sorted
*/
void selectionSort(vector<Bid>& bids) {
//define min as int (index of the current minimum bid)
unsigned int smallest;
// check size of bids vector
// set size_t platform-neutral result equal to bid.size()
unsigned int largest = bid.size();
// pos is the position within bids that divides sorted/unsorted
// for size_t pos = 0 and less than size -1
// set min = pos
// loop over remaining elements to the right of position
// if this element's title is less than minimum title
// this element becomes the minimum
// swap the current minimum with smaller one found
// swap is a built in vector method
for (unsigned place = 0; place < largest; ++place) {
smallest = place;
for (unsigned j = place + 1; j < largest; ++j) {
if (bids[j].title.compare(bids[smallest].title) < 0) {
smallest = j;
}
if (smallest != place) {
swap(bids[place], bids[smallest]);
}
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*/
int main(int argc, char* argv[]) {
// process command line arguments
string csvPath;
switch (argc) {
case 2:
csvPath = argv[1];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
}
// Define a vector to hold all the bids
vector<Bid> bids;
// Define a timer variable
clock_t ticks;
int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Load Bids" << endl;
cout << " 2. Display All Bids" << endl;
cout << " 3. Selection Sort All Bids" << endl;
cout << " 4. Quick Sort All Bids" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
// Initialize a timer variable before loading bids
ticks = clock();
// Complete the method call to load the bids
bids = loadBids(csvPath);
cout << bids.size() << " bids read" << endl;
// Calculate elapsed time and display result
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 2:
// Loop and display the bids read
for (int i = 0; i < bids.size(); ++i) {
displayBid(bids[i]);
}
cout << endl;
break;
// FIXME (1b): Invoke the selection sort and report timing results
case 3: //quickSort is utilized to sort loaded bids and elapsed time is determined & displayed
ticks = clock();
cout << bids.size() << " bids read" << endl;
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
// FIXME (2b): Invoke the quick sort and report timing results
case 4: //quickSort is utilized to sort loaded bids and elapsed time is determined & displayed
ticks = clock();
quickSort(bids, 0, bids.size() - 1);
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
}
}
cout << "Good bye." << endl;
return 0;
}