-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.c
executable file
·51 lines (45 loc) · 875 Bytes
/
bin.c
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
// Binary search
#include<stdio.h>
#include<stdlib.h>
int binary_search(int a[],int low,int key,int high)
{
unsigned int mid,midVal;
while(low<=high)
{
mid = ((unsigned)low+(unsigned)high) >> 1;
//mid = low+((high+low)/2);
printf("\n Mid Index %d",mid);
midVal = a[mid];
if(midVal<key)
low = mid+1;
else if(midVal>key)
high = mid-1;
else
return mid;
}
return -1;
}
int main()
{
unsigned int a[1000000],i,key,n,b,d;
printf("\n Enter the Number of elements ");
scanf("%d",&n);
if(n==0){
printf("\n No Elements" );
exit(0);
}
printf("\n Elements of the array \n");
for(i=1;i<=n;i++)
{
a[i]=i;
printf("\n%u",a[i]);
}
printf("\n enter the key value");
scanf("%d",&key);
b= binary_search(a,1,key,n);
if(b==-1)
printf("\n Key not found\n");
else
printf("\n Key found at position \n%d\n", b);
return 0;
}