-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartI_D.cpp
331 lines (264 loc) · 8.95 KB
/
PartI_D.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
/**********************************************
* PART I.4: Interpolation Search improvements *
**********************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.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
struct dailyStockData
{
char Date[11];
float Open, High, Low, Close;
int Volume, OpenInt;
};
typedef struct dailyStockData dataItem;
int (*searchAlgPtr)(dataItem arr[], int l, int r, char x[]); // Pointer to functions implementing interpolationSearch algorithms
int steps; // Number of steps made by algorithm
// Declaration of functions
int readFile(dataItem arr[], int argc, char *argv[]);
int binInterpolationSearch(dataItem arr[], int l, int r, char x[]);
int binInterpolationSearchImproved(dataItem arr[], int l, int r, char x[]);
int binarySearchOnJumps(dataItem arr[], int l, int r, int arrSize, char x[]);
int linearSearch(dataItem arr[], int l, int r, char x[]);
unsigned long val(char s[]);
int max(int a, int b);
int min(int a, int b);
int main(int argc, char *argv[])
{
dataItem S[MAX_SIZE];
if (argc >= 2)
{
if (strcmp(argv[1], "binInterpolationSearch") == 0)
searchAlgPtr = &binInterpolationSearch;
else if (strcmp(argv[1], "binInterpolationSearchImproved") == 0)
searchAlgPtr = &binInterpolationSearchImproved;
else
{
printf("Invalid algorithm specified. Please use 'binInterpolationSearch' or 'binInterpolationSearchImproved'.\n");
return 1;
}
}
else
{
printf("Please specify the sorting algorithm to use as a command line argument. Options: 'binInterpolationSearch', 'binInterpolationSearchImproved'.\n");
return 1;
}
int N = readFile(S, argc, argv); // N is the number of daily transactions read from file
char x[11];
int pos; // Position of search key in the array or -1 if not found
printf("Give the date to search for (yyyy-mm-dd): ");
scanf("%s", x);
typedef std :: chrono :: high_resolution_clock clock;
// Start measuring running time
auto startTime = clock :: now();
pos = (*searchAlgPtr)(S, 0, N-1, x);
// 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 (searchAlgPtr == &binInterpolationSearch)
printf("\n\n[BINARY INTERPOLATION SEARCH]\n");
if (searchAlgPtr == &binInterpolationSearchImproved)
printf("\n\n[BINARY INTERPOLATION SEARCH IMPROVED]\n");
if (pos == -1)
printf("\nDate %s not found.\nNumber of steps: %d\n", x, steps);
else
printf("\nDate %s found at array position %d. Volume: %d\nNumber of steps: %d\n", x, pos, S[pos].Volume, steps);
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
}
// Helping function to convert a Date field to its integer value
unsigned long val(char s[])
{
int i, j, k = 0;
char d[strlen(s)-2];
i = strlen(s);
for (j=0; j<i; j++)
{
if (isdigit(s[j]))
{
d[k] = s[j];
k++;
}
}
d[k] = '\0';
return (unsigned long)atoi(d);
}
// Utility function
int max(int a, int b)
{
return a >= b ? a : b;
}
// Utility function
int min(int a, int b)
{
return a <= b ? a : b;
}
// Binary interpolation search algorithm
int binInterpolationSearch(dataItem arr[], int l, int r, char x[])
{
int arrSize, m, j;
steps = 1;
while (strcmp(arr[l].Date, x) < 0 && strcmp(arr[r].Date, x) >= 0)
{
arrSize = r - l + 1;
/* if (arrSize <= 5)
return linearSearch(arr, l, r, x); */
m = l + (r-l) * (val(x) - val(arr[l].Date)) / (val(arr[r].Date) - val(arr[l].Date)); // probe index
j = 1; // In each loop iteration j counts the sqrt(arrSize) size jumps
steps++;
if (strcmp(arr[m].Date, x) < 0)
{
while (strcmp(arr[min((int)(m + j * sqrt(arrSize)), r)].Date, x) < 0)
{
j++; steps++;
}
l = m + (j - 1) * sqrt(arrSize) + 1;
r = min(m + j * sqrt(arrSize), r);
}
else
{
if (strcmp(arr[m].Date, x) > 0)
{
while (strcmp(arr[max((int)(m - j * sqrt(arrSize)), l)].Date, x) > 0)
{
j++; steps++;
}
l = max(m - j * sqrt(arrSize), l);
r = m - (j - 1) * sqrt(arrSize) - 1;
}
else
l = m;
}
}
if (strcmp(x, arr[l].Date) == 0)
return l; // Search successful
else
return -1; // Search unsuccessful
}
// Modified binary search algorithm to locate the block of array arr of size arrSize containing x. In the standard binary search algorithm arrSize = 0 and after call we check S[pos]
int binarySearchOnJumps(dataItem arr[], int l, int r, int arrSize, char x[])
{
int m;
while (l + arrSize <= r)
{
steps++;
m = (l+r)/2; // probe index
if (strcmp(arr[m].Date, x) == 0)
return m;
else if (strcmp(arr[m].Date, x) < 0)
l = m+1;
else
r = m-1;
}
return l;
}
// Improved binary interpolation search with exponential size jumps
int binInterpolationSearchImproved(dataItem arr[], int l, int r, char x[])
{
int arrSize, m, j;
steps = 1;
while (strcmp(arr[l].Date, x) < 0 && strcmp(arr[r].Date, x) >= 0)
{
arrSize = r - l + 1;
/* if (arrSize <= 5)
return linearSearch(arr, l, r, x); */
m = l + (r-l) * (val(x) - val(arr[l].Date)) / (val(arr[r].Date) - val(arr[l].Date)); // probe index
j = 0; // In each loop iteration j counts the exponential size jumps
steps++;
if (strcmp(arr[m].Date, x) < 0)
{
while (strcmp(arr[min((int)(m + pow(2, j) * sqrt(arrSize)), r)].Date, x) < 0)
{
j++; steps++;
}
l = j > 0 ? m + pow(2, j - 1) * sqrt(arrSize) + 1 : m + 1;
r = min(m + pow(2, j) * sqrt(arrSize), r);
if (j > 1) // Call binary search for block sizes 2*sqrt(arrSize), 4*sqrt(arrSize), 8*sqrt(arrSize), ...
{
l = binarySearchOnJumps(arr, l, r, (int)(sqrt(arrSize)), x);
r = min(l + sqrt(arrSize), r);
}
}
else
{
if (strcmp(arr[m].Date, x) > 0)
{
while (strcmp(arr[max((int)(m - pow(2, j) * sqrt(arrSize)), l)].Date, x) > 0)
{
j++; steps++;
}
l = max(m - pow(2, j) * sqrt(arrSize), l);
r = j > 0 ? r = m - pow(2, j - 1) * sqrt(arrSize) - 1 : m - 1;
if (j > 1) // Call binarySearchOnJumps only if block size >= 2*sqrt(arrSize)
{
l = binarySearchOnJumps(arr, l, r, (int)(sqrt(arrSize)), x);
r = min(l + sqrt(arrSize), r);
}
}
else
l = m;
}
}
if (strcmp(x, arr[l].Date) == 0)
return l; // Search successful
else
return -1; // Search unsuccessful
}
// Linear search algorithm
int linearSearch(dataItem arr[], int l, int r, char x[])
{
int i = l;
while (i <= r && strcmp(x, arr[i].Date) >= 0)
{
steps++;
if (strcmp(x, arr[i].Date) == 0)
return i;
i++;
}
return -1;
}