-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion-merge.cpp
421 lines (294 loc) · 7.8 KB
/
insertion-merge.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
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
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctime>
#include <fstream>
#include <climits> // ULLONG_MAX
using namespace std;
class Product {
public:
char timestamp[27];
char price[10];
int order;
double price_conv;
unsigned long long int time_modified;
void convert_time_to_integer();
};
void Product::convert_time_to_integer(){
char *ptr = timestamp;
char *x;
char string_time[14] = {'\0'};
string_time[0] = ptr[9];
string_time[1] = ptr[11];
string_time[2] = ptr[12];
string_time[3] = ptr[14];
string_time[4] = ptr[15];
string_time[5] = ptr[17];
string_time[6] = ptr[18];
string_time[7] = ptr[20];
string_time[8] = ptr[21];
string_time[9] = ptr[22];
string_time[10] = ptr[23];
string_time[11] = ptr[24];
string_time[12] = ptr[25];
time_modified = strtoull(string_time, &x, 10);
}
class Storage {
Product *product_list;
int number;
public:
Storage(int num) : product_list(NULL) , number(num){} // Constructor
~Storage();
void fetch_from_CSV();
void shuffle();
void sort_by_INSERTION(char*);
void sort_by_MERGE(char*);
void write_to_CSV();
void print_storage();
private:
void split_string(char*, char*, char*); // fetch_from_CSV() will call this
void insertion_sort_t(); // Private insertion sort function called by main sort_by_INSERTION() function according to timestamp
void insertion_sort_p(); // Private insertion sort function called by main sort_by_INSERTION() function according to last price
void merge_sort_t(int, int); // Private merge sort function called by main sort_by_MERGE() function according to timestamp
void merge_sort_p(int, int); // Private merge sort function called by main sort_by_MERGE() function according to last price
void merging_t(int, int, int);
void merging_p(int, int, int);
};
void Storage::insertion_sort_t(){
Product *x = product_list;
Product temp;
int i, j;
for( i = 1; i < number; i++ ){
temp = x[i];
j = i - 1;
while( j >= 0 && (x[j].time_modified > temp.time_modified) ){
x[j+1] = x[j];
j = j - 1;
}
x[j+1] = temp;
}
}
void Storage::insertion_sort_p(){
Product *x = product_list;
Product temp;
int i, j;
for( i = 1; i < number; i++ ){
temp = x[i];
j = i -1;
while( j >= 0 && (x[j].price_conv > temp.price_conv) ){
x[j+1] = x[j];
j = j - 1;
}
x[j+1] = temp;
}
}
void Storage::merge_sort_t(int p, int r){
int q;
Product *x = product_list;
if(p < r){
q = (p+r)/2;
merge_sort_t( p, q );
merge_sort_t( q+1, r );
merging_t(p, q, r);
}
}
void Storage::merging_t(int p, int q, int r){
Product *x = product_list;
int n1 = q-p+1;
int n2 = r-q;
Product *Left = new Product [n1 + 1];
Product *Right = new Product [n2 + 1];
for(int i = 0; i < n1; i++){
Left[i] = x[p+i];
}
Left[n1].time_modified = ULLONG_MAX;
for(int j = 0; j < n2; j++){
Right[j] = x[q+j+1];
}
Right[n2].time_modified = ULLONG_MAX;
int i = 0;
int j = 0;
for( int k = p; k <= r; k++ ){
if( Left[i].time_modified < Right[j].time_modified ){
x[k] = Left[i];
i++;
}
else {
x[k] = Right[j];
j++;
}
}
delete [] Left;
delete [] Right;
}
void Storage::merge_sort_p(int p, int r){
int q;
Product *x = product_list;
if(p < r){
q = (p+r)/2;
merge_sort_p( p, q );
merge_sort_p( q+1, r );
merging_p(p, q, r);
}
}
void Storage::merging_p(int p, int q, int r){
Product *x = product_list;
int n1 = q-p+1;
int n2 = r-q;
Product *Left = new Product [n1 + 1];
Product *Right = new Product [n2 + 1];
for(int i = 0; i < n1; i++){
Left[i] = x[p+i];
}
Left[n1].price_conv = 1000000000000000000.0;
for(int j = 0; j < n2; j++){
Right[j] = x[q+j+1];
}
Right[n2].price_conv = 1000000000000000000.0;
int i = 0;
int j = 0;
for( int k = p; k <= r; k++ ){
if( Left[i].price_conv < Right[j].price_conv ){
x[k] = Left[i];
i++;
}
else {
x[k] = Right[j];
j++;
}
}
delete [] Left;
delete [] Right;
}
void Storage::fetch_from_CSV(){
product_list = new Product [number];
ifstream CSV_File("log_inf.csv");
string single_line;
char *char_line;
getline(CSV_File, single_line, '\n'); // disposing of the tags
for(int i = 0; i < number; i++){
for(int j = 0; j < 27; j++){ product_list[i].timestamp[j] = 0; }
for(int k = 0; k < 10; k++){ product_list[i].price[k] = 0; }
getline(CSV_File, single_line, '\n');
char_line = new char [single_line.size()+1];
strcpy(char_line, &single_line[0]);
split_string(product_list[i].timestamp, product_list[i].price, char_line);
product_list[i].convert_time_to_integer();
product_list[i].price_conv = atof(product_list[i].price);
product_list[i].order = i+1;
}
}
void Storage::shuffle(){
srand (time(NULL));
int random = 0;
Product temp;
for(int i = 0; i < number; i++){
random = rand() % number;
temp = product_list[i];
product_list[i] = product_list[random];
product_list[random] = temp;
}
}
void Storage::sort_by_INSERTION(char *var){
char c = *var;
if( c == 't' ){
cout << "\n\nInsertion Sort will be run by Timestamp\n\n";
insertion_sort_t();
}
else if( c == 'p' ){
cout << "\n\nInsertion Sort will be run by Price\n\n";
insertion_sort_p();
}
else {
cout << "ERROR: Insertion sort could not be run..!!!";
}
}
void Storage::sort_by_MERGE(char *var){
char c = *var;
if( c == 't' ){
cout << "\n\nMerge Sort will be run by Timestamp\n\n";
merge_sort_t(0, number-1);
}
else if( c == 'p' ){
cout << "\n\nMerge Sort will be run by Price\n\n";
merge_sort_p(0, number-1);
}
else {
cout << "ERROR: Insertion sort could not be run..!!!";
}
}
Storage::~Storage(){
delete [] product_list;
number = 0;
}
void Storage::split_string(char *tm, char *prc, char *line){
char *front = line;
char *tail = line;
while( *front == ' ' ){
front++;
tail++;
}
while( *front != ',' ){
front++;
}
while( tail != front ){
*tm = *tail;
tm++;
tail++;
}
tm = '\0';
while(*front == ','){
front++;
}
while( *front != ','){
front++;
}
while( *front == ','){
front++;
}
tail = front;
while( *front != ','){
front++;
}
while( tail != front ){
*prc = *tail;
prc++;
tail++;
}
prc = '\0';
}
void Storage::print_storage(){
cout << "\n\t\t\t\t\t\tTimestamp\t\t\tLast Price\n\n";
for( int i = 0; i < number; i++ ){
cout << " Actual order in CSV: " << product_list[i].order << "\t\t:" << product_list[i].timestamp << ":" << "\t\t" << ":" << product_list[i].price << ":\n\n";
}
}
int main(int argc, char *argv[]){
int size = 1000;
char request[] = "t";
char b = 'i';
clock_t t1, t2;
Storage Database(size);
Database.fetch_from_CSV();
Database.shuffle();
cout << "\n\n\nDo you want to sort the DATABASE..? If yes, press ENTER: " << endl;
getchar();
if( b == 'i' ){
t1 = clock();
Database.sort_by_INSERTION(request);
t2 = clock();
cout << "\n\nMicroseconds elapsed for " << size << " elements in INSERTION SORT: " << t2 - t1 << "\n\n";
}
else if (b == 'm'){
t1 = clock();
Database.sort_by_MERGE(request);
t2 = clock();
cout << "\n\nMicroseconds elapsed for " << size << " elements in MERGE SORT: " << t2 - t1 << "\n\n";
}
cout << "\n\n\nSorting is over, do you want to print the DATABASE..? If yes, press ENTER: " << endl;
getchar();
Database.print_storage();
getchar();
return 0;
}