-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsorter.cpp
More file actions
138 lines (120 loc) · 3.25 KB
/
Copy pathsorter.cpp
File metadata and controls
138 lines (120 loc) · 3.25 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
/**
* @file
* @author The CS2 TA Team
* @version 1.0
* @date 2013-2014
* @copyright This code is in the public domain.
*
* @brief The bubble sort, quick sort, merge sort, and in-place quicksort
* algorithms (implementation).
*
*/
#include "sorter.hpp"
int main(int argc, char* argv[])
{
// Set up buffers and data input
std::vector<int> nums;
std::string line;
char *filename;
int sort_type;
// Ensure that at most one type of sort and at least a filename are specified.
if (argc > 3 || argc < 2)
{
usage();
}
// default sort is bubble sort
sort_type = BUBBLE_SORT;
// Figure out which sort to use
for (int i = 1; i < argc; ++i)
{
char *arg = argv[i];
if (strcmp(arg, "-b") == 0) { sort_type = BUBBLE_SORT; }
else if (strcmp(arg, "-q") == 0) { sort_type = QUICK_SORT; }
else if (strcmp(arg, "-m") == 0) { sort_type = MERGE_SORT; }
else if (strcmp(arg, "-qi") == 0) { sort_type = QUICK_SORT_INPLACE; }
else { filename = argv[i]; }
}
// Read the file and fill our vector of integers
// THIS FUNCTION IS STUDENT IMPLEMENTED
readFile(filename, nums);
switch (sort_type)
{
case BUBBLE_SORT:
print_vector(bubbleSort(nums));
break;
case QUICK_SORT:
print_vector(quickSort(nums));
break;
case MERGE_SORT:
print_vector(mergeSort(nums));
break;
case QUICK_SORT_INPLACE:
quicksort_inplace(nums, 0, nums.size() - 1);
print_vector(nums);
break;
default:
usage();
break;
}
return 0;
}
/**
* Usage Prints out a usage statement and exits.
*/
void usage()
{
std::cerr << usage_string << std::endl;
exit(1);
}
/**
* TO STUDENTS: In all of the following functions, feel free to change the
* function arguments and/or write helper functions as you see fit. Remember to
* add the function header to sorter.hpp if you write a helper function!
*/
/**
* TODO: Implement this function.
*/
std::vector<int> bubbleSort(std::vector<int> &list)
{
return list;
}
/**
* TODO: Implement this function.
*/
std::vector<int> quickSort(std::vector<int> &list)
{
return list;
}
/**
* TODO: Implement this function.
*/
std::vector<int> mergeSort(std::vector<int> &list)
{
return list;
}
/**
* TODO: Implement this function.
*/
std::vector<int> merge(std::vector<int> &left, std::vector<int> &right)
{
return left;
}
/*
* quicksort_inplace: In-place version of the quicksort algorithm. Requires
* O(1) instead of O(N) space, same time complexity. Each call of
* the method partitions the list around the pivot (an item taken
* from the middle of the array) with items left of the pivot
* smaller than it and items to its right larger than it. Then the
* method recursively sorts the left and right portions of the list
* until it reaches its base case: a list of length 1 is already
* sorted.
*
* @param list: pointer to integer array to be sorted
* @returns: Nothing, the array is sorted IN-PLACE.
*
* TODO: Implement this function.
*/
void quicksort_inplace(std::vector<int> &list, int left, int right)
{
return;
}