-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.binary.c
More file actions
49 lines (48 loc) · 695 Bytes
/
5.binary.c
File metadata and controls
49 lines (48 loc) · 695 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
#include<stdio.h>
int bin(int a[],int,int,int);
main()
{
int a[50],i,j,n,high,low;
printf("\nEnter the no of elements\t");
scanf("%d",&n);
printf("\nEnter the elements\t");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{for(j=i;j<n;j++)
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
low=0;high=n;
printf("\nEnter the no to be searched\t");
scanf("%d",&j);
bin(a,high,low,j);
}
int bin(int a[],int high,int low,int j)
{
int mid;
mid=(high+low)/2;
if(a[mid]==j)
{
printf("found\n");
return j;
}
if(high==low)
{
printf("NOt found\n");
return(1);
}
if(j<a[mid])
{
high=mid-1;
}
if(j>a[mid])
{
low=mid+1;
}
bin(a,high,low,j);
}