-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBINARY_S.C
More file actions
51 lines (42 loc) · 748 Bytes
/
BINARY_S.C
File metadata and controls
51 lines (42 loc) · 748 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
#include<stdio.h>
#include<conio.h>
void binary_search(int a[],int n,int value);
void main()
{
int a[10],n,value,i;
clrscr();
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the element of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value that you want search\n");
scanf("%d",&value);
binary_search(a,n,value);
getch();
}
void binary_search(int a[],int n,int value)
{
int loc=-1,low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(value==a[mid])
{
loc=mid+1;
break;
}
else if(value>a[mid])
low=mid+1;
else
high=mid-1;
}
if(loc==-1)
printf("%d number is not found",value);
else
printf("%d found at location %d",value,loc);
}