-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
182 lines (169 loc) · 3.45 KB
/
search.c
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
/*!
* Native C implementations of popular search algorithms.
* Author: Athanasios Salamanis
* Project start date: Tuesday 02/11/2021
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
/*!
* Returns the minimum of two integer values.
* @param v1 the first integer value.
* @param v2 the second integer value.
* @return the minimum of the two integer values.
*/
int min(int v1, int v2)
{
return (v1 < v2) ? v1 : v2;
}
/*!
* Implements the linear search algorithm.
* @param arr the sorted array in which a value is searched.
* @param n the number of values in the sorted array.
* @param val the value we are searching for.
* @return the index of the searched value if it is contained in the array, -1 otherwise.
*/
int linearSearch(int* arr, int n, int val)
{
bool found = false;
int i = 0;
for (i = 0; i < n; ++i)
{
if (arr[i] == val)
{
found = true;
break;
}
}
if (found)
{
return i;
}
else
{
return - 1; // key not found
}
}
/*!
* Implements the binary search algorithm.
* @param arr the sorted array in which a value is searched.
* @param n the number of values in the sorted array.
* @param val the value we are searching for.
* @return the index of the searched value if it is contained in the array, -1 otherwise.
*/
int binarySearch(int* arr, int n, int val)
{
// l, r, m stand for left, right and middle, respectively
int l = 0;
int r = n - 1;
int m = -1;
while (l <= r)
{
m = floor((l + r) / 2);
if (val < arr[m])
{
r = m - 1;
}
else if (val > arr[m])
{
l = m + 1;
}
else
{
return m;
}
}
return -1; // key not found
}
/*!
* Implements the jump search algorithm.
* @param arr the sorted array in which a value is searched.
* @param n the number of values in the sorted array.
* @param val the value we are searching for.
* @return the index of the searched value if it is contained in the array, -1 otherwise.
*/
int jumpSearch(int* arr, int n, int val)
{
int a = 0;
int b = floor(sqrt(n));
while (arr[min(b, n) - 1] < val)
{
a = b;
b += floor(sqrt(n));
if (a >= n)
{
return -1; // key not found
}
}
while (arr[a] < val)
{
a += 1;
if (a == min(b, n))
{
return -1; // key not found
}
}
if (arr[a] == val)
{
return a;
}
else
{
return -1; // key not found
}
}
/*!
* The start point of application. Just for demonstration purposes, i.e., the algorithms themselves can be embeded into a library.
*/
int main(int argc, char const *argv[])
{
int algorithmID = atoi(argv[1]);
int n = atoi(argv[2]);
int val = atoi(argv[3]);
int* arr = malloc(n * sizeof(int));
for (int i = 0; i < n; ++i)
{
arr[i] = i;
}
double elapsedTime = -1.0;
int keyIndex = -1;
switch (algorithmID)
{
case 1: // linear search
{
clock_t start = clock();
keyIndex = linearSearch(arr, n, val);
clock_t end = clock();
elapsedTime = (double)(end - start) / CLOCKS_PER_SEC;
break;
}
case 2: // binary search
{
clock_t start = clock();
keyIndex = binarySearch(arr, n, val);
clock_t end = clock();
elapsedTime = (double)(end - start) / CLOCKS_PER_SEC;
break;
}
case 3: // jump search
{
clock_t start = clock();
keyIndex = jumpSearch(arr, n, val);
clock_t end = clock();
elapsedTime = (double)(end - start) / CLOCKS_PER_SEC;
break;
}
}
if (keyIndex == -1)
{
printf("Key not found.\n");
}
else
{
printf("Key found in index %d in %f seconds.\n", keyIndex, elapsedTime);
}
return 0;
}