-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysort.cpp
More file actions
457 lines (360 loc) · 11.3 KB
/
mysort.cpp
File metadata and controls
457 lines (360 loc) · 11.3 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <cstdlib> // std::rand, std::srand
#include <stdio.h>
#include <getopt.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <ctime> /* clock_t, clock, CLOCKS_PER_SEC */
#include <string>
#include <random>
#include <sstream> // std::stringstream
using namespace std;
void print_usage(){
printf("Usage: mysort -h <Help> -r <minrange,maxrange> -s <selection sort> -u <uniform distribution> -m <merge sort> -q <quicksort>\n");
}
void printHelpGuid(){
printf("The -r is a mandatory option flag and must be followed a 2 non-negative numbers separated by a comma specifiy the range of values that mysort initializes. E.G mysort -r 10, 50\n");
printf("The -u an optional parameter flag specifies that the list to be created be uniformly distributed.\n");
printf("The -s is a parameter flag that specifies mysort sort the item list using selection sort.\n");
printf("The -m is a parameter flag that specifies mysort sort the item list using merge sort.\n");
printf("The -q is a parameter flag that specifies mysort sort the item list using quicksort.\n");
printf("\n");
}
int findSmallest(const vector<int> & nums, int start){
int size = (int)nums.size();
int minIdx = start;
for(;start < size; ++start){
if(nums[minIdx] > nums[start]){
minIdx = start;
}
}
return minIdx;
}
bool linearSearch(const vector<int> & nums, int item){
for (int i = 0; i < nums.size(); ++i){
if(item == nums[i]){
return true;
}
}
return false;
}
bool interpolationSearch(const vector<int> & nums, int item){
int low = 0;
int pos;
int high = nums.size()-1;
while (low <= high && item >= nums[low] && item <= nums[high])
{
// Probing the position with keeping
// uniform distribution in mind.
int pos = low + (((double)(high - low) / (nums[high] - nums[low])) * (item - nums[low]));
// Condition of target found
if (nums[pos] == item)
return true;
// If item is larger, item is in upper part
if (nums[pos] < item)
low = pos + 1;
// If item is smaller, item is in the lower part
else
high = pos - 1;
}
return false;
}
bool binarySearch(const vector<int> & nums, int item){
int low = 0;
int high = nums.size()-1;
int mid;
while(low <= high){
mid = low + (high - low)/2;
if(nums[mid] < item){
low = mid +1;
}
else if (nums[mid] > item){
high = mid -1;
}
else {
return true;
}
}
return false;
}
vector<int> selectionSort(const vector<int> & nums){
vector<int> sortArry(nums.size());
copy(nums.begin(), nums.end(), sortArry.begin());
printf("\n--------------------Selection Sort----------------------------\n");
int bestIdx;
for (size_t i = 0; i < sortArry.size(); ++i){
bestIdx = findSmallest(sortArry, i);
swap(sortArry[bestIdx], sortArry[i]);
}
return sortArry;
}
vector <int> quickSort(const vector<int> & nums){
vector<int> Arry(nums.size());
copy(nums.begin(), nums.end(), Arry.begin());
printf("\n---------------------Quick Sort--------------------------------\n");
return Arry;
}
void merge(vector<int> & nums, int low, int mid, int high){
int leftstart = low;
int rightstart = mid+1;
int size = high - low + 1;
int index = -1;
vector<int> temp (size);
while(leftstart <= mid && rightstart <= high){
if(nums[leftstart] <= nums[rightstart]){
temp[++index] = nums[leftstart];
++leftstart;
}
else {
temp[++index] = nums[rightstart];
++rightstart;
}
}
++index;
copy(nums.begin() + leftstart, nums.begin() + mid+1, temp.begin() + index);
copy(nums.begin() + rightstart, nums.begin() + high+1, temp.begin() + index);
copy(temp.begin(), temp.end(), nums.begin() + low);
}
void mergesort(vector<int> & nums, int low, int high){
if(low == high){
return;
}
int mid = low + (high - low)/2;
mergesort(nums, low, mid);
mergesort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
vector<int> mergeSort(const vector<int> & nums){
vector<int> Arry(nums.size());
copy(nums.begin(), nums.end(), Arry.begin());
printf("\n---------------------Merge Sort--------------------------------\n");
mergesort(Arry, 0, nums.size()-1);
return Arry;
}
/**
* @param string, min value and max value seperated by a comma
* uses the min and max value to determine the size and the range of values in the list
* @return vector<int>, a vector of integers with the min and max value specified by the string
*/
vector <int> randomize_vector(string range, bool UFlag){
char delimiter = ',';
size_t pos = range.find(delimiter);
int range1 = stoi(range.substr(0, pos));
int end = range.size();
// Check if the end user entered second delimeter
size_t pos2 = range.find(delimiter, pos + 1);
//The item wasn't found
if(pos2 == string::npos){
pos2 = range.size();
}
int range2 = stoi(range.substr(pos + 1, pos2));
int minrange = (range1 <= range2) ? range1: range2;
int maxrange = (range1 <= range2) ? range2: range1;
int capacity = maxrange - minrange+1;
vector<int> nums(capacity);
int size = -1;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(minrange, maxrange);
vector<bool> Bitset(capacity);
for (int i = 0; i < capacity; ++i){
int number = dis(gen);
if(!UFlag || (UFlag && !Bitset[number - minrange]))
nums[++size] = number;
Bitset[number-minrange] = true;
}
nums.resize(++size);
return nums;
}
int grabIn(const int maxattempts, int ifFail, string message){
int option;
int attempts = 0;
cin >> option;
bool cinFail = cin.fail();
while(cinFail){
++attempts;
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
printf("Invalid input. Try again. You have %d attempts remaining.\n", maxattempts - attempts);
if(attempts == maxattempts){
return ifFail;
}
printf("%s ", message.c_str());
cin >> option;
cinFail = cin.fail();
}
return option;
}
void searchMethods(const vector<int> & nums, string message ){
bool isValid = false;
bool found;
int option, searchItem, userChoice;
int attempts = 0;
isValid = false;
clock_t start, end;
double elasped;
do {
printf("Enter 0 to perform a linear search\n");
printf("Enter 1 to perform a binary seach\n");
printf("Enter 2 to perform an interpolation search\n");
printf("Enter 3 to exit\n");
stringstream ss("Enter option: ");
printf("Enter option: ");
userChoice = grabIn(5, -1, ss.str());
int ifFail = nums[0]-1;
if(userChoice != -1 && userChoice != 3){
printf("Enter number to be searched: ");
ss.str("Enter number to be searched: ");
searchItem = grabIn(3, ifFail, ss.str());
}
option = (ifFail == searchItem || userChoice == -1) ? -1 : userChoice;
printf("-----------------------------------------------------------------------------------\n");
printf("%s ", message.c_str());
switch(option){
case 0:
start = clock();
found = linearSearch(nums, searchItem );
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC * 1000;
if(found)
printf("Linear search was able to");
else
printf("Linear search wasn't able to");
printf(" locate the item in %.6f milliseconds\n", elasped);
break;
case 1:
start = clock();
found = binarySearch(nums, searchItem );
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC * 1000;
if(found)
printf("Binary search was able to");
else
printf("Binary search wasn't able to");
printf(" locate the item in %.6f milliseconds\n", elasped);
break;
case 2:
start = clock();
found = interpolationSearch(nums, searchItem );
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC * 1000;
if(found)
printf("Interpolation search was able to");
else
printf("Interpolation search wasn't able to");
printf(" locate the item in %.6f milliseconds\n", elasped);
break;
case -1:
printf("Error too failed attempts. Exiting.\n");
case 3:
isValid = true;
break;
}
}while(!isValid);
}
void parseArguments(const vector<char> & commands, vector<int> & nums){
const int viewSize = 50;
vector<int> arry;
std::stringstream ss;
clock_t start, end;
double elasped;
if(nums.size() <= viewSize){
printf("Do you wish view if the contents of the vector. Enter 1 for yes: ");
ss.str("Do you wish view if the contents of the vector. Enter 1 for yes: ");
int usrRep = grabIn(1, 0, ss.str());
if(usrRep)
copy(nums.begin(), nums.end(), ostream_iterator<int>(cout, ", "));
printf("\n");
}
for(int i = 0; i < commands.size(); ++i){
switch(commands[i]){
case 's':
start = clock();
arry = selectionSort(nums);
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC;
ss << "Selection Sort took "<< elasped << " seconds to complete. ";
searchMethods(arry, ss.str());
ss.str(std::string());
//copy(arry.begin(), arry.end(), ostream_iterator<int>(cout, ", "));
break;
case 'q':
printf("Quick Sort hasn't been implemented yet.\n");
/*
start = clock();
arry = quickSort(nums);
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC;
ss << "Quick Sort took "<< elasped << " seconds to complete. ";
searchMethods(arry, ss.str());
*/
break;
case 'm':
start = clock();
arry = mergeSort(nums);
end = clock();
elasped = (double) (end-start) / CLOCKS_PER_SEC;
ss << "Merge Sort took "<< elasped << " seconds to complete. ";
searchMethods(arry, ss.str());
ss.str(std::string());
break;
}
}
}
int main(int argc, char * argv []){
const int maxargcs = 2;
if(argc < maxargcs){
print_usage();
}
int opt;
vector<char> commands;
vector<int> nums;
string size;
bool UFlag = false;
bool HFlag = false;
// Parse command line arguments
while((opt = getopt(argc, argv, ":r:mshqu")) != -1){
switch(opt){
case 'r': // initialize a vector of size optarg
size = optarg;
if(!isdigit(size[0])){
print_usage();
}
break;
case 'u':
UFlag = true;
break;
case 'h':
HFlag = true;
break;
case 's':
case 'q':
case 'm':
commands.push_back(opt);
break;
case ':': // The user entered a flag but not an associated value
printf(" Error. Option needs a value\n");
print_usage();
exit(2);
break;
case '?': // The user entered an unknown flag
printf("Unknown option: %c\n", optopt);
print_usage();
exit(2);
break;
}
}
if(HFlag ){
print_usage();
if(commands.empty()){
printHelpGuid();
exit(0);
}
else
exit(2);
}
nums = randomize_vector(size, UFlag);
parseArguments(commands, nums);
}