C program for binary search


#include<stdio.h>
#include<conio.h>
#define max 5
//f => first
//l => last
//m => middle
main()
{
int temp,array[max],i,ii,value,f,l,m,j;
clrscr();
printf(":plz enter %d value in array:\n",max);
for(i=0;i<max;i++)
{ printf("ok enter new number: ");
scanf("%d",&array[i]);
}
printf(":\nplz enter value for searching:\n");
scanf("%d",&value);

f=0;
l=max-1;
m=(f+l)/2;
while(f<=l)
{
if(value>array[m])
{
f=m+1;
}
else if(value==array[m])
{
printf("%d found at location %d", value, m+1);
break;
}
else
{
l=m-1;
}
m=(f+l)/2;
}

if(f>l)
printf(":not found:");
//printf("\n%d\n",f);
//printf("%d",l);
getch();

}

Comments

Popular posts from this blog