-
Notifications
You must be signed in to change notification settings - Fork 119
/
33.c
36 lines (33 loc) · 875 Bytes
/
33.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
#include <stdio.h>
int search(int A[], int n, int target) {
int l, r, m;
l = 0;
r = n - 1;
while (l <= r) {
m = (l + r) / 2;
if (A[m] == target) return m;
if (A[l] <= A[m]) { /* left half is sorted */
if (A[l] <= target && target < A[m]) { /* target in left half */
r = m - 1;
}
else {
l = m + 1;
}
}
else { /* right half is sorted */
if (A[m] < target && target <= A[r]) { /* target in right half */
l = m + 1;
}
else {
r = m - 1;
}
}
}
return -1;
}
int main() {
int num[] = { 1, 3, 5 };
/* should be 0 */
printf("%d\n", search(num, sizeof(num)/sizeof(num[0]), 1));
return 0;
}