forked from Heatwave/The-C-Programming-Language-2nd-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.binsearch.c
92 lines (76 loc) · 1.54 KB
/
01.binsearch.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
#include <stdio.h>
#include <time.h>
#define ARRAY_LEN 500000
int binsearch(int, int[], int);
int binsearchFast(int, int[], int);
main()
{
clock_t start, finish;
int i, v[ARRAY_LEN];
for (i = 0; i < ARRAY_LEN; i++)
v[i] = i;
start = clock();
int p;
for (i = 0; i < ARRAY_LEN; i++) {
p = binsearch(i, v, ARRAY_LEN);
//printf("%d at %d\n", i, p);
}
finish = clock();
printf("binsearch duration: %f\n", ((double)(finish - start)) / CLOCKS_PER_SEC);
//printf("binsearchFast:\n");
start = clock();
for (i = 0; i < ARRAY_LEN; i++) {
p = binsearchFast(i, v, ARRAY_LEN);
//printf("%d at %d\n", i, p);
}
finish = clock();
printf("binsearchFast duration: %f\n", ((double)(finish - start)) / CLOCKS_PER_SEC);
for (i = -10000; i < ARRAY_LEN; i++) {
p = binsearchFast(i, v, ARRAY_LEN);
if (i < 0 && p != -1)
break;
else if (i >= 0 && p != i)
break;
}
printf("i: %d\n", i);
}
/* binsearch: find x in v[0] <= v[1] <= ... v[n-1] */
int binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (x < v[mid])
high = mid - 1;
else if (x > v[mid])
low = mid + 1;
else
return mid;
}
return -1;
}
int binsearchFast(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (x <= v[mid])
high = mid - 1;
else
low = mid + 1;
}
if (x == v[mid])
return mid;
else if (x > v[mid])
mid += 1;
else
mid -= 1;
if (mid < 0 || mid > n - 1)
return -1;
else
return mid;
}