forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_search.c
More file actions
39 lines (35 loc) · 746 Bytes
/
Binary_search.c
File metadata and controls
39 lines (35 loc) · 746 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
#include<stdio.h>
int A[50];
int binary_search(int s, int f, int val);
int main(){
int n, val, x, i;
printf("enter the value of n: ");
scanf("%d", &n);
printf("enter the values in array: ");
for(i=0; i<n; i++){
scanf("%d", &A[i]);
}
printf("enter the value to be searched: ");
scanf("%d", &val);
x=binary_search(0, n-1, val);
printf("%d", x);
return 0;
}
int binary_search(int s, int f, int val){
int m;
if(s==f & A[s]!=val)
return 0;
if(s!=f){
m=(f+s)/2;
if(A[m]==val)
return 1;
if(val < A[m]){
f=m-1;
return binary_search(s, f, val);
}
if(val > A[m]){
s=m+1;
return binary_search(s, f, val);
}
}
}