-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.binsearch.c
40 lines (37 loc) · 898 Bytes
/
3.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
#include "stdio.h"
int binsearch(int v[], int x, int n) {
int low = 0;
int high = n - 1;
int mid;
while (low <= high) {
mid = (low + high) / 2;
if (v[mid] < x) {
low = mid + 1;
} else if (v[mid] > x) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
int binsearch2(int v[], int x, int start, int end) {
if (start > end) {
return -1;
}
int mid = (start + end) / 2;
if (v[mid] < x) {
return binsearch2(v, x, mid + 1, end);
} else if (v[mid] > x) {
return binsearch2(v, x, start, mid - 1);
} else {
return mid;
}
}
int main() {
int v[] = {1, 2, 3, 4, 5, 6};
printf("%d\n", binsearch(v, 4, 6));
printf("%d\n", binsearch(v, 0, 6));
printf("%d\n", binsearch2(v, 4, 0, 5));
printf("%d\n", binsearch2(v, 0, 0, 5));
}