-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
71 lines (48 loc) · 1000 Bytes
/
binary_search.cpp
File metadata and controls
71 lines (48 loc) · 1000 Bytes
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
#include"essentials.cpp"
int binary_search(int array[],int size,int low, int high,int key){
if(low>=high){
return -1;
}
while(low<=high){
int m=low+(high-low)/2;
if(key>array[m]){
low=m+1;
high=size-1;
}
else if(key<array[m]){
low=0;
high=m-1;
}
else{
return m;
}
}
}
int binary_search_recursive(int array[], int low, int high, int x){
if(low>high){
return -1;
}
else{
int mid=low+ (high-low)/2;
if(array[mid]==x){
return mid;
}
else if(x<array[mid]){
return binary_search_recursive(array,low,mid-1,x);
}
else if(x>array[mid]){
return binary_search_recursive(array,mid+1,high,x);
}
}
}
int main(){
int array[6]={1,4,5,3,2,9};
int inorder[]={8,3,1,10,6,4,7,14,13};
int sixe=sizeof(inorder)/sizeof(inorder[0]);
int size1=sizeof(array);
int f;
f=binary_search_recursive(inorder,0,sixe-1,10);
cout<<f;
// printf("%d",f);
return 0;
}