-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.1.c
87 lines (79 loc) · 1.29 KB
/
3.1.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
#include <stdio.h>
/*
* 3.1 improve the given binary search algorithm
*/
int binsearch(int, int [], int);
int kr_binsearch(int, int [], int);
int
kr_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 /* found match */
return mid;
}
return -1; /* no match */
}
int
binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
mid = (low + high) / 2;
while ((low <= high) && (x != v[mid])) {
if (x < v[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
mid = (low + high) / 2;
}
if (x == v[mid])
return mid;
else
return -1;
}
int
binsearch2(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;
else
low = mid + 1;
}
if (x == v[low])
return low;
else
return -1;
}
int
main(void)
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int n = sizeof(a) / sizeof(int);
int q = -1;
scanf("%d", &q);
int exit_status = 0;
int idx = -1;
if ((idx = binsearch2(q, a, n)) != -1) {
printf("at %d\n", idx);
exit_status = 0;
} else {
printf("not found\n");
exit_status = 1;
}
return exit_status;
}