-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
262 lines (214 loc) · 6.81 KB
/
MergeSort.cpp
File metadata and controls
262 lines (214 loc) · 6.81 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
#include "MergeSort.h"
int main( int argc, char* argv[] ){
string inFile = "";
bool printHelp = false;
for(int i = 1; i < argc; i++){
bool validCmd = false;
string cmd = argv[i];
if( !strcmp(argv[i],"--inFile") ){
if( checkOpt(argv[i],argv[i+1]) ) { inFile = argv[i+1]; } validCmd = true; }
else if( !strcmp(argv[i],"--useArray") ){ m_useArr = true; validCmd = true; }
else if( !strcmp(argv[i],"--debug") ){ m_debug = true; validCmd = true; }
else if( !strcmp(argv[i],"--verbose") ){ m_verbose = true; validCmd = true; }
else if( !strcmp(argv[i],"--help") ){ printHelp = true; validCmd = true; }
if( !validCmd && cmd.find("--")!=string::npos){
cout << "\033[1;31m ERROR:: Option " << cmd << " not valid. Type ./MergeSort"
<< " --help and check the available options, exit!\033[0m" << endl;
exit(0);
}
}
if( printHelp ){
cout << "MergeSort Algorithm: sort an array of integers" << endl;
cout << " usage: ./MergeSort [--help] [--inFile] [--useArray]" << endl;
cout << " [--debug] [--verbose]" << endl;
cout << " arguments (default value): " << endl;
cout << " --help print this help and exit" << endl;
cout << " --inFile define input file with the array (None)" << endl;
cout << " --useArray enable using array instead of vector (false)" << endl;
cout << " --debug enable debug mode (false)" << endl;
cout << " --verbose enable verbose mode (false)" << endl;
exit(0);
}
vector<int> vecList;
int num = 0;
if(inFile=="" ){
cout << " WARNING:: inFile is a needed argument!! \n"
<< " Please type name and path to it" << endl;
cin >> inFile;
}
m_inStream.open( inFile.c_str() );
if( !m_inStream.good() ){
cout << " ERROR:: inFile " << inFile << " doesn't exist \n"
<< " Please enter the array manually! \n"
<< " Type the size of the array " << endl;
int size = 0;
cin >> size;
for(int i=0; i<size; i++){
cout << " Type an integer " << endl;
cin >> num;
vecList.push_back( num );
num = 0;
}
}
else{
cout << " File containing the array is : " << inFile << endl;
while( m_inStream.good() ){
m_inStream>>num;
vecList.push_back(num);
num=0;
}
}
m_inStream.close();
int arrList[vecList.size()];
if(m_useArr){
for(uint i=0; i<vecList.size(); i++){
arrList[i] = vecList[i];
}
if(vecList.size()!=sizeof(arrList) / sizeof(*arrList)){
cout << " ERROR:: wrong conversion from vector to array! Exiting" << endl;
exit(0);
}
vecList.clear();
}
if(m_debug){
if(m_useArr){
cout << " Size of the array = " << sizeof(arrList) / sizeof(*arrList) << endl;
cout << " DEBUG:: values in the array are : " << endl;
printArray(arrList,sizeof(arrList) / sizeof(*arrList));
}
else{
cout << " Size of the vector = " << vecList.size() << endl;
cout << " DEBUG:: values in the vector are: " << endl;
printVector(vecList);
}
}
if(m_useArr){
mergeSort(arrList,0,sizeof(arrList) / sizeof(*arrList) -1);
if(m_debug){
cout << " DEBUG:: values on the sorted array are: " << endl;
printArray(arrList,sizeof(arrList) / sizeof(*arrList));
}
}
else{
vector<int> sortedVector = mergeSort(vecList);
if(m_debug){
cout << " DEBUG:: values on the sorted array are: " << endl;
printVector(sortedVector);
}
}
return 0;
}
vector<int> mergeSort(vector<int> array){
// base case: if size 1 return vector
if( array.size() == 1){
return array;
}
// Determine the location of the middle element in the vector
vector<int>::iterator middle = array.begin() + (array.size() / 2);
// define left and right halves
vector<int> left ( array.begin(), middle );
vector<int> right( middle , array.end() );
// Perform a merge sort on the two smaller vectors
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
vector<int> merge(const vector<int> &left, const vector<int> &right){
// Fill the resultant vector with sorted results from both vectors
vector<int> result;
unsigned left_it = 0, right_it = 0;
while( left_it < left.size() && right_it < right.size() ){
// If the left value is smaller than the right it goes next
// into the resultant vector
if( left[left_it] < right[right_it] ){
result.push_back(left[left_it]);
left_it++;
}
else{
result.push_back(right[right_it]);
right_it++;
}
}
// Push the remaining data from both vectors onto the resultant
while( left_it < left.size() ){
result.push_back(left[left_it]);
left_it++;
}
while( right_it < right.size() ){
result.push_back(right[right_it]);
right_it++;
}
return result;
}
void mergeSort(int *array, int left, int right){
if(left<right){
//find middle of the array
int mid = (right+left)/2;
// Sort left and right halves
mergeSort(array, left, mid);
mergeSort(array, mid+1, right);
// merge halves
merge(array, left, mid, right);
}
}
void merge(int *array, int left, int mid, int right){
int nl = mid-left+1;
int nr = right-mid;
/* create temp arrays */
int L[nl], R[nr];
/* Copy data to temp arrays L[] and R[] */
for(int ii=0; ii<nl; ii++)
L[ii] = array[left+ii];
for(int jj=0; jj<nr; jj++)
R[jj] = array[mid+1+jj];
/* Merge the temp arrays back into arr[l..r]*/
int i = 0; // Initial index of left subarray
int j = 0; // Initial index of right subarray
int k = left; // Initial index of merged subarray
while (i<nl && j<nr){
if (L[i]<=R[j]){
array[k] = L[i];
i++;
}
else{
array[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L[], if any
while(i<nl){
array[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[], if any
while(j<nr){
array[k] = R[j];
j++;
k++;
}
}
bool checkOpt(char* opt, char* value){
std::string val = value;
if( 0==val.find("--") ){
cout << "WARNING:: Option " << opt <<
" requires value -> skipping " << endl;
return false;
}
return true;
}
void printArray(int *array, int size){
for(int i=0; i<size; i++){
if(i==0) cout << " " << array[i];
else cout << " , " << array[i];
}
cout << endl;
}
void printVector(vector<int> vec){
for(uint i=0; i<vec.size(); i++){
if(i==0) cout << " " << vec[i];
else cout << " , " << vec[i];
}
cout << endl;
}