-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexesices3.c
More file actions
67 lines (64 loc) · 1.11 KB
/
Copy pathexesices3.c
File metadata and controls
67 lines (64 loc) · 1.11 KB
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
//3.1
#include <stdio.h>
int search(int n,int[] b,int k){
int i,j,p;
i = 0;
j = n-1;
while(i <= j){
p = (i+j)/2; //binary search
if(b[p]<=k){
i = p + 1;
}else{
j = p - 1;
}
}
if(j >= 0 && b[j] == k){
return 1;
}else{
return 0;
}
}
int main(){
int a[] = {3,6,7,10,13,15,19};
if(search(7,a,14)){
printf("......");
}else{
printf("......");
}
retrn 0;
}
//3.2(1ß)
int bin_search(int a[],int n,int key){
int l,m,u;
l = 0; u = n - 1;
while(l <= u){
m = (l + u) / 2;
if(a[m] < key)
l = m + 1;
else if(a[m] > key)
u = m - 1;
else
return m;
}
return -1;
}
//3.2(3)
int bin_search_first(int a[], int n, int key){
int l, m, u;
int p;
l = -1; u = n;
while(l + 1 != u){
m = (l + u) / 2;
if(a[m] < key){
l = m;
}else{
u = m;
}
}
p = u;
if(p == n/*l == n-1*/|| a[p] != key){
return -1;
}else{
return p;
}
}