forked from ericwang14/Programming-Pearls-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
284 lines (248 loc) · 5.28 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
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
/* Copyright (C) 1999 Lucent Technologies */
/* From 'Programming Pearls' by Jon Bentley */
/* search.c -- test and time binary and sequential search
Select one of three modes by editing main() below.
1.) Probe one function
2.) Test one function extensively
3.) Time all functions
Input lines: algnum n numtests
Output lines: algnum n numtests clicks nanosecs_per_elem
See timedriver for algnum codes
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXN 1000000
typedef int DataType;
DataType x[MAXN];
int n;
/* Scaffolding */
int i = -999999;
#define assert(v) { if ((v) == 0) printf(" binarysearch bug %d %d\n", i, n); }
/* Alg 1: From Programming Pearls, Column 4: raw transliteration */
int binarysearch1(DataType t)
{ int l, u, m;
l = 0;
u = n-1;
for (;;) {
if (l > u)
return -1;
m = (l + u) / 2;
if (x[m] < t)
l = m+1;
else if (x[m] == t)
return m;
else /* x[m] > t */
u = m-1;
}
}
/* Alg 2: Make binarysearch1 more c-ish */
int binarysearch2(DataType t)
{ int l, u, m;
l = 0;
u = n-1;
while (l <= u) {
m = (l + u) / 2;
if (x[m] < t)
l = m+1;
else if (x[m] == t)
return m;
else /* x[m] > t */
u = m-1;
}
return -1;
}
/* Alg 3: From PP, Col 8 */
int binarysearch3(DataType t)
{ int l, u, m;
l = -1;
u = n;
while (l+1 != u) {
m = (l + u) / 2;
if (x[m] < t)
l = m;
else
u = m;
}
if (u >= n || x[u] != t)
return -1;
return u;
}
/* Alg 4: From PP, Col 9 */
int binarysearch4(DataType t)
{ int l, p;
if (n != 1000)
return binarysearch3(t);
l = -1;
if (x[511] < t) l = 1000 - 512;
if (x[l+256] < t) l += 256;
if (x[l+128] < t) l += 128;
if (x[l+64 ] < t) l += 64;
if (x[l+32 ] < t) l += 32;
if (x[l+16 ] < t) l += 16;
if (x[l+8 ] < t) l += 8;
if (x[l+4 ] < t) l += 4;
if (x[l+2 ] < t) l += 2;
if (x[l+1 ] < t) l += 1;
p = l+1;
if (p >= n || x[p] != t)
return -1;
return p;
}
/* Alg 9: Buggy, from Programming Pearls, Column 5 */
int sorted()
{ int i;
for (i = 0; i < n-1; i++)
if (x[i] > x[i+1])
return 0;
return 1;
}
int binarysearch9(DataType t)
{ int l, u, m;
/* int oldsize, size = n+1; */
l = 0;
u = n-1;
while (l <= u) {
/* oldsize = size;
size = u - l +1;
assert(size < oldsize); */
m = (l + u) / 2;
/* printf(" %d %d %d\n", l, m, u); */
if (x[m] < t)
l = m;
else if (x[m] > t)
u = m;
else {
/* assert(x[m] == t); */
return m;
}
}
/* assert(x[l] > t && x[u] < t); */
return -1;
}
/* Alg 21: Simple sequential search */
int seqsearch1(DataType t)
{ int i;
for (i = 0; i < n; i++)
if (x[i] == t)
return i;
return -1;
}
/* Alg 22: Faster sequential search: Sentinel */
int seqsearch2(DataType t)
{ int i;
DataType hold = x[n];
x[n] = t;
for (i = 0; ; i++)
if (x[i] == t)
break;
x[n] = hold;
if (i == n)
return -1;
else
return i;
}
/* Alg 23: Faster sequential search: loop unrolling */
int seqsearch3(DataType t)
{ int i;
DataType hold = x[n];
x[n] = t;
for (i = 0; ; i+=8) {
if (x[i] == t) { break; }
if (x[i+1] == t) { i += 1; break; }
if (x[i+2] == t) { i += 2; break; }
if (x[i+3] == t) { i += 3; break; }
if (x[i+4] == t) { i += 4; break; }
if (x[i+5] == t) { i += 5; break; }
if (x[i+6] == t) { i += 6; break; }
if (x[i+7] == t) { i += 7; break; }
}
x[n] = hold;
if (i == n)
return -1;
else
return i;
}
/* Scaffolding to probe one algorithm */
void probe1()
{ int i;
DataType t;
while (scanf("%d %d", &n, &t) != EOF) {
for (i = 0; i < n; i++)
x[i] = 10*i;
printf(" %d\n", binarysearch9(t));
}
}
/* Torture test one algorithm */
#define s seqsearch3
void test(int maxn)
{ int i;
for (n = 0; n <= maxn; n++) {
printf("n=%d\n", n);
/* distinct elements (plus one at top) */
for (i = 0; i <= n; i++)
x[i] = 10*i;
for (i = 0; i < n; i++) {
assert(s(10*i) == i);
assert(s(10*i - 5) == -1);
}
assert(s(10*n - 5) == -1);
assert(s(10*n) == -1);
/* equal elements */
for (i = 0; i < n; i++)
x[i] = 10;
if (n == 0) {
assert(s(10) == -1);
} else {
assert(0 <= s(10) && s(10) < n);
}
assert(s(5) == -1);
assert(s(15) == -1);
}
}
/* Timing */
int p[MAXN];
void scramble(int n)
{ int i, j;
DataType t;
for (i = n-1; i > 0; i--) {
j = (RAND_MAX*rand() + rand()) % (i + 1);
t = p[i]; p[i] = p[j]; p[j] = t;
}
}
void timedriver()
{ int i, algnum, numtests, test, start, clicks;
while (scanf("%d %d %d", &algnum, &n, &numtests) != EOF) {
for (i = 0; i < n; i++)
x[i] = i;
for (i = 0; i < n; i++)
p[i] = i;
scramble(n);
start = clock();
for (test = 0; test < numtests; test++) {
for (i = 0; i < n; i++) {
switch (algnum) {
case 1: assert(binarysearch1(p[i]) == p[i]); break;
case 2: assert(binarysearch2(p[i]) == p[i]); break;
case 3: assert(binarysearch3(p[i]) == p[i]); break;
case 4: assert(binarysearch4(p[i]) == p[i]); break;
case 9: assert(binarysearch9(p[i]) == p[i]); break;
case 21: assert(seqsearch1(p[i]) == p[i]); break;
case 22: assert(seqsearch2(p[i]) == p[i]); break;
case 23: assert(seqsearch3(p[i]) == p[i]); break;
}
}
}
clicks = clock() - start;
printf("%d\t%d\t%d\t%d\t%g\n",
algnum, n, numtests, clicks,
1e9*clicks/((float) CLOCKS_PER_SEC*n*numtests));
}
}
/* Main */
int main()
{ /* probe1(); */
/* test(25); */
timedriver();
return 0;
}