Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program is used to carry out binary search. (C++)


#include< iostream.h>
#include< conio.h>
int binary_search(int array[],int value,int size)
{
int found =0;
inthigh=size,low=0,mid;
mid=(high+low)/2;
cout< < "\n\nLooking for "< < value< < endl;
while((!found) && (high>=low))
{
cout< < "Low "< < low< < " Mid "< < mid< < " High "< < high< < endl;
if (value==array[mid])
found=1;
else if(value< array[mid])
high=mid-1;
else low=mid+1;
mid=(high+low)/2;
}
return((found)?mid: -1);
}
void main(void)
{
clrscr();
int array[100],i;
for(i=0;i< 100;i++)
array[i]=i+20;
cout< < "Result of search "< < binary_search(array,15,100)< < endl;
cout< < "Result of search "< < binary_search(array,23,100)< < endl;
}

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP