PROGRAM:
#include
#include
#define size 10
int n;
void main()
{
int a[size],key,i,flag;
int binsearch(int a[size],int key);
clrscr();
printf("\n How many elements for an array");
scanf("%d",&n);
printf("Enter the Elements");
for(i=0;i
scanf("%d",&a[i]);
printf("\n Enter the no which is to be searched");
scanf("%d",&key);
flag=binsearch(a,key);
if(flag==-1)
printf("\nThe element is not Present");
else
printf("\n The element is a[%d] localtion",flag);
getch();
}
int binsearch(int a[size],int key)
{
int low,high,m ;
low=0;
high=n-1;
while(low<=high)
{
m=(low+high)/2;
if(key==a[m])
return m;
else if(key
high=m-1;
else
low=m+1;
}
return -1;
}
OUTPUT:
How many elements for an array 5
Enter the Elements
1
2
3
4
5
Enter the no which is to be searched 4
The element is a [3] location
CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )