Saturday, November 5, 2011

Binary Search in C language

Binary Search in C language

#include<conio.h>
#include<stdio.h>
 void main()
 {
int arr[10]={34,56,12,345,2,65,89,34,5,12};
int i,j,temp;
int beg=0,end=9,mid,item;

clrscr();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n");
for(i=0;i<10;i++)
{
printf(" %d",arr[i]);
}
printf("\nEnter number to find  ");
scanf("%d",&item);
while(beg<=end)
{
mid=int((end+beg)/2);
if(item==arr[mid])
{
printf("\nNumber is present\nLocation=%d",mid);
break;
}
else
{
if(arr[mid]>item)
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
}
if(beg>end)
{
printf("\nNumber is not present....");
}
getch();
 }

0 comments:

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.