EmbLogic's Blog

SIMPLEX TYPE CODE OF BINARY SEARCH

/*condition for binary search is this that… u have to use sorted array & u should know size of array in which element is to be searched otherwise do sorting 1st*/
#include<stdio.h>
int main()
{
int search;
int hi=10,lw=0,mid=0,i,k,proceed;
int arr[10];//{3,5,6,7,9,10,13,14,16,17};
for(k=0;k<10;k++)
{
printf(“\nGIVE ME ARRAY:”);
scanf(“%d”,&arr[k]);
}
i=hi;
for(proceed=0;i>1;proceed++)//inti then cond check ….back……1st it inc.then check cond.
{
i=i/2;
//pro=3
}
printf(“\nproceed=%d”,proceed);
do
{
printf(“\nenter the element to be searched:”);
scanf(“%d”,&search);
mid=0;
hi=10;lw=0;
mid=(hi+lw)/2;
for(i=0;i<proceed;i++)//loop should operate at 0,1& 2 times.
{
//mid=(hi+lw)/2;
if(arr[mid]>search)
{
hi=mid;
mid=(lw+hi)/2;
//printf(“\nmid in if=%d”,mid);

}
else if(arr[mid]<search)
{
lw=mid;//now func will work in else for these initialised values.
mid=(lw+hi)/2;//7th pos=14//again come back at 8th pos
//printf(“\nmid in else if=%d”,mid);

}
if(arr[mid]==search)//by using else if here one loop will proceed extra again by proceed=3 value.
{
printf(“\nsearched at %d pos”,mid);
break;

}
}
}while(search != 0);

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>