-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingMerge.cpp
270 lines (219 loc) · 8.36 KB
/
CountingMerge.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
bool isLeapYear(int year);
int getDaysInMonth(int month, int year);
int daysBetweenDate(int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear);
void merge(vector<int> &numofdays, int left, int mid, int right, vector<int> &day, vector<int> &month, vector<int> &year, vector<long> &value);
void mergeSort(vector<int> &numofdays, int left, int right, vector<int> &day, vector<int> &month, vector<int> &year, vector<long> &value);
void countingSort(vector<int>& numofdays, int length, vector<int>& day, vector<int>& month, vector<int>& year, vector<long>& value);
int main(){
ifstream file("file.csv");
if (!file.is_open()){
cout<<"Error opening file"<<endl;
return 1;
}
int records=0, i=0;
vector<int> day, month, year, numofdays;
vector<long> value;
string line;
while(getline(file, line)){
vector<string> row;
stringstream ss(line);
string cell;
while(getline(ss, cell, ','))
row.push_back(cell);
//extracting day, month and year from the string
stringstream ss1(row[2]);
string dayStr, monthStr, yearStr;
getline(ss1, dayStr, '/');
getline(ss1, monthStr, '/');
getline(ss1, yearStr, '/');
day.push_back(stoi(dayStr));
month.push_back(stoi(monthStr));
year.push_back(stoi(yearStr));
value.push_back(stol(row[8]));
records ++;
}
for(i=0; i<records; i++)
numofdays.push_back(daysBetweenDate(day[0], month[0], year[0], day[i], month[i], year[i]));
int choice; //letting the user decide which algorithm he wants to use
do{
cout<<"Press 1 to use Merge Sort or press 2 to use Counting Sort in order to sort the dates: "<<endl;
cin>>choice;
} while(choice!=1 && choice!=2);
if(choice==1)
mergeSort(numofdays, 0, records-1, day, month, year, value);
else
countingSort(numofdays, records, day, month, year, value);
for(i=0; i<records; i++)
cout<<"Date: "<<day[i]<<"/"<<month[i]<<"/"<<year[i]<<" -> Value: "<<value[i]<<"\t";
if(choice==1)
cout<<endl<<endl<<"Merge Sort was executed successfully";
else
cout<<endl<<endl<<"Counting Sort was executed successfully";
return 0;
}
bool isLeapYear(int year){
return(year%4==0 && year%100!=0) || (year%400==0);
}
int getDaysInMonth(int month, int year){
if (month==2)
return isLeapYear(year)?29:28;
else if (month==4 || month==6 || month==9 || month==11)
return 30;
else
return 31;
}
int daysBetweenDate(int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear){
int days=0;
for(int year=0; year<startYear; year++)
days-=isLeapYear(year)?366:365;
for(int month=1; month<startMonth; month++)
days-=getDaysInMonth(month, endYear);
days-=startDay;
for(int year=0; year<endYear; year++)
days+=isLeapYear(year)?366:365;
for(int month=1; month<endMonth; month++)
days += getDaysInMonth(month, endYear);
days+=endDay;
int daysBetween=days;
return daysBetween;
}
void merge(vector<int> &numofdays, int left, int mid, int right, vector<int> &day, vector<int> &month, vector<int> &year, vector<long> &value){
int n1=mid-left+1;
int n2=right-mid;
//Creating temporary vectors to store the elements of the two subarrays
vector<int> leftnum, rightnum;
vector<int> leftday, rightday;
vector<int> leftmonth, rightmonth;
vector<int> leftyear, rightyear;
vector<long> leftvalue, rightvalue;
//Copying the elements from the original arrays to the temporary arrays
for(int i=0; i<n1; i++){
leftnum.push_back(numofdays[left + i]);
leftday.push_back(day[left + i]);
leftmonth.push_back(month[left + i]);
leftyear.push_back(year[left + i]);
leftvalue.push_back(value[left + i]);
}
for(int j=0; j<n2; j++){
rightnum.push_back(numofdays[mid + 1 + j]);
rightday.push_back(day[mid + 1 + j]);
rightmonth.push_back(month[mid + 1 + j]);
rightyear.push_back(year[mid + 1 + j]);
rightvalue.push_back(value[mid + 1 + j]);
}
int i=0,j=0,k=left;
//Merging the temporary arrays back into the original arrays in sorted order
while(i<n1 && j<n2){
if(leftnum[i]==rightnum[j]){
if(leftvalue[i]<=rightvalue[j]){
numofdays[k]=leftnum[i];
day[k]=leftday[i];
month[k]=leftmonth[i];
year[k]=leftyear[i];
value[k]=leftvalue[i];
i++;
}
else{
numofdays[k]=rightnum[j];
day[k]=rightday[j];
month[k]=rightmonth[j];
year[k]=rightyear[j];
value[k]=rightvalue[j];
j++;
}
}
else{
if(leftnum[i]<rightnum[j]){
numofdays[k]=leftnum[i];
day[k]=leftday[i];
month[k]=leftmonth[i];
year[k]=leftyear[i];
value[k]=leftvalue[i];
i++;
}
else{
numofdays[k]=rightnum[j];
day[k]=rightday[j];
month[k]=rightmonth[j];
year[k]=rightyear[j];
value[k]=rightvalue[j];
j++;
}
}
k++;
}
//Copying the remaining elements of the left subarray (if there are any)
while(i<n1){
numofdays[k]=leftnum[i];
day[k]=leftday[i];
month[k]=leftmonth[i];
year[k]=leftyear[i];
value[k]=leftvalue[i];
i++;
k++;
}
//Copying the remaining elements of the right subarray (if there are any)
while(j<n2){
numofdays[k]=rightnum[j];
day[k]=rightday[j];
month[k]=rightmonth[j];
year[k]=rightyear[j];
value[k]=rightvalue[j];
j++;
k++;
}
}
void mergeSort(vector<int> &numofdays, int left, int right, vector<int> &day, vector<int> &month, vector<int> &year, vector<long> &value){
if(left<right){
int mid=left+(right-left)/2;
//Recursive calling mergeSort to divide the array into subarrays
mergeSort(numofdays, left, mid, day, month, year, value);
mergeSort(numofdays, mid + 1, right, day, month, year, value);
//Merging the sorted subarrays
merge(numofdays, left, mid, right, day, month, year, value);
}
}
void countingSort(vector<int>& numofdays, int length, vector<int>& day, vector<int>& month, vector<int>& year, vector<long>& value){
int i, maxElement=0;
for(i=0; i<length; i++){
if(numofdays[i]>maxElement)
maxElement = numofdays[i];
}
//Creating a counting array of size maxNumofDays + 1
vector<int> counter;
for(i=0; i<=maxElement+1; i++)
counter.push_back(0);
//Counting the number of appearances of each numofdays
for(i=0; i<length; i++)
counter[numofdays[i]]++;
//Adding the count of the previous element to the count of the current element
for(i=1; i<=maxElement; i++)
counter[i]+=counter[i-1];
//Creating temporary arrays to store the sorted elements
vector<int> outputnum(length);
vector<int> outputday(length), outputmonth(length), outputyear(length);
vector<long> outputvalue(length);
//Sorting the elements based on the count array
for(i=length-1; i>=0; i--){
outputnum[counter[numofdays[i]] - 1] = numofdays[i];
outputday[counter[numofdays[i]] - 1] = day[i];
outputmonth[counter[numofdays[i]] - 1] = month[i];
outputyear[counter[numofdays[i]] - 1] = year[i];
outputvalue[counter[numofdays[i]] - 1] = value[i];
counter[numofdays[i]]--;
}
//Copying the sorted elements back to the original arrays
for(i=0; i<length; i++){
numofdays[i] = outputnum[i];
day[i] = outputday[i];
month[i] = outputmonth[i];
year[i] = outputyear[i];
value[i] = outputvalue[i];
}
}