-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartI_B.cpp
316 lines (250 loc) · 8.14 KB
/
PartI_B.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
/*************************************************
* PART I.2: Heapsort and Countingsort algorithms *
**************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <chrono> // C++ library to measure the running time of each algorithm using the clock with the highest resolution available
#include <unistd.h>
#define MAX_SIZE 3240 // Max size of the array stored the daily transactions
// Data record read from file
struct dailyStockData
{
char Date[11];
float Open, High, Low, Close;
int Volume, OpenInt;
};
typedef struct dailyStockData dataItem;
void (*sortAlgPtr)(dataItem arr[], int arrSize); // Pointer to functions implementing heapSort and countingSort algorithms
int comps = 0; // Number of comparisons made by heapSort
// Declaration of functions
int readFile(dataItem arr[], int argc, char *argv[]);
void heapSort(dataItem arr[], int arrSize);
void shiftDown(dataItem arr[], int i, int k);
void countingSort(dataItem arr[], int arrSize);
void countingSortExtended(dataItem arr[], int arrSize);
int cmpCloseDate(dataItem a, dataItem b);
void swap(dataItem *a, dataItem *b);
void printArray(dataItem arr[], int arrSize);
int main(int argc, char *argv[])
{
dataItem S[MAX_SIZE];
if (argc >= 2)
{
if (strcmp(argv[1], "heapSort") == 0)
sortAlgPtr = &heapSort;
else if (strcmp(argv[1], "countingSort") == 0)
sortAlgPtr = &countingSort;
else if (strcmp(argv[1], "countingSortExtended") == 0)
sortAlgPtr = &countingSortExtended;
else
{
printf("Invalid algorithm specified. Please use 'heapSort', 'countingSort', or 'countingSortExtended'.\n");
return 1;
}
}
else
{
printf("Please specify the sorting algorithm to use as a command line argument. Options: 'heapSort', 'countingSort', 'countingSortExtended'.\n");
return 1;
}
int N = readFile(S, argc, argv); // N is the number of daily transactions read from file
typedef std :: chrono :: high_resolution_clock clock;
// Start measuring running time
auto startTime = clock :: now();
(*sortAlgPtr)(S, N);
// Stop measuring running time and calculate the elapsed time
auto endTime = clock :: now();
auto elapsedTime = std :: chrono :: duration_cast<std :: chrono :: nanoseconds>(endTime - startTime).count();
if (sortAlgPtr == &heapSort)
printf("[HEAPSORT] ");
else if (sortAlgPtr == &countingSort)
printf("[COUNTINGSORT] ");
else if (sortAlgPtr == &countingSortExtended)
printf("[COUNTINGSORT EXTENDED] ");
printf("SORTED ARRAY:\n\n");
printArray(S, N);
if (sortAlgPtr == &heapSort)
printf("\n\nNumber of comparisons: %d\n", comps);
printf("Running time measured: %lg seconds\n", (double)elapsedTime * 1e-9);
return 0;
}
// Open the file, read data records, store them to array arr and return the number of records read
int readFile(dataItem arr[], int argc, char *argv[])
{
FILE *fp;
char *fileName;
char line[80];
int numLines = 0; // Number of lines read from file
dataItem dt;
if (argc >= 3) // Data filename passed as a command line argument
fileName = strdup(argv[2]);
else
{
printf("Give the stock data filename: "); // Data filename asked by user
scanf("%ms", &fileName);
printf("\n\n");
}
// Check if the file exists
if (access(fileName, F_OK) == -1)
{
printf("\nERROR: File '%s' not found\n", fileName);
free(fileName);
exit(1);
}
fp = fopen(fileName, "r");
if (!fp) // fp == NULL
{
printf("\nERROR: can't open file\n");
free(fileName);
exit(1);
}
fgets(line, 80, fp); // Get the first line
while (fgets(line, 80, fp))
{
sscanf(line, "%10s,%f,%f,%f,%f,%d,%d", dt.Date, &dt.Open, &dt.High, &dt.Low, &dt.Close, &dt.Volume, &dt.OpenInt);
arr[numLines] = dt;
numLines++;
}
free(fileName);
fclose(fp);
return numLines; // Number of daily transactions read
}
// Print array data records
void printArray(dataItem arr[], int arrSize)
{
printf("Date : \tOpen\tHigh \tLow \tClose \tVolume\n");
printf("--------------------------------------------------------\n");
for (int i = 0; i < arrSize; i++)
printf("%s : \t%.3f\t%.3f\t%.3f\t%.3f\t%d\n", arr[i].Date, arr[i].Open, arr[i].High, arr[i].Low, arr[i].Close, arr[i].Volume);
}
/* // Print Close field of array data records - It is used for the screenshots
void printArray(dataItem arr[], int arrSize)
{
for (int i = 0; i < arrSize; i++)
printf("%.3f | ", arr[i].Close);
} */
// Utility function to swap the contents of two data records
void swap(dataItem *a, dataItem *b)
{
dataItem temp;
temp = *a;
*a = *b;
*b = temp;
}
// Heapsort algorithm
void heapSort(dataItem arr[], int arrSize)
{
// Build the heap
for (int i = arrSize/2; i > 0; i--)
shiftDown(arr, i, arrSize);
// Move the largest element from root to rightmost leaf of the bottom level and rebuild the heap
for (int i = arrSize; i > 1; i--)
{
swap(&arr[i-1], &arr[0]);
shiftDown(arr, 1, i-1);
}
}
// Shift down the root element in order to satisfy the heap property
void shiftDown(dataItem arr[], int root, int last)
{
int j, k;
dataItem v;
v = arr[root-1];
k = root;
while (k <= last/2)
{
j = 2*k;
if (j < last)
{
comps++;
if (cmpCloseDate(arr[j], arr[j-1]) > 0)
j++;
}
comps++;
if (cmpCloseDate(v, arr[j-1]) >= 0)
break;
arr[k-1] = arr[j-1];
k = j;
}
arr[k-1] = v;
}
// Utility function to compare pairs (Close, Date)
int cmpCloseDate(dataItem a, dataItem b)
{
if (a.Close > b.Close)
return 1;
if (a.Close < b.Close)
return -1;
if (strcmp(a.Date, b.Date) > 0)
return 1;
if (strcmp(a.Date, b.Date) < 0)
return -1;
return 0;
}
// Counting sort algorithm on non negative integer values
void countingSort(dataItem arr[], int arrSize)
{
int i, j, v, max, min;
max = arr[0].Close;
min = max;
for (i = 1; i < arrSize; i++) // Compute max, min of the values (int) value of Close field
{
v = arr[i].Close;
if (v > max)
max = v;
if (v < min)
min = v;
}
// C[i] finally counts the int values smaller than or equal to i+min
int C[max-min+1] = {0}; // Initialize array C to 0
for (j = 0; j < arrSize; j++)
{
v = arr[j].Close;
C[v-min]++;
}
for (i = 1; i < max-min+1; i++)
C[i] = C[i] + C[i-1];
dataItem B[arrSize]; // B stores the sorted data records
for (j = arrSize-1; j >= 0; j--)
{
v = arr[j].Close;
B[C[v-min]-1] = arr[j];
C[v-min]--;
}
for (i = 0; i < arrSize; i++)
arr[i] = B[i]; // Move temp array B to array arr
}
// Counting sort algorithm on non negative integer values extended
void countingSortExtended(dataItem arr[], int arrSize)
{
int i, j, v, UpMax, UpMin;
UpMax = 1000 * arr[0].Close;
UpMin = UpMax;
for (i = 1; i < arrSize; i++) // Compute UpMax, UpMin of the values 1.000 * value of Close field
{
v = 1000 * arr[i].Close;
if (v > UpMax)
UpMax = v;
if (v < UpMin)
UpMin = v;
}
// C[i] finally counts the number of int values smaller than or equal to i+UpMin
int C[UpMax-UpMin+1] = {0}; // Initialize C array to 0
for (j = 0; j < arrSize; j++)
{
v = 1000 * arr[j].Close;
C[v-UpMin]++;
}
for (i = 1; i < UpMax-UpMin+1; i++)
C[i] = C[i] + C[i-1];
dataItem B[arrSize];
for (j = arrSize-1; j >= 0; j--)
{
v = 1000 * arr[j].Close;
B[C[v-UpMin]-1] = arr[j];
C[v-UpMin]--;
}
for (i = 0; i < arrSize; i++)
arr[i] = B[i]; // Move B to arr
}