Tuesday, October 14, 2008

7.13 PROGRAM TO IMPLEMENT BINARY SEARCH

#include
#include

int mid,i,arr[20],l=0,p,max,se;
int binsrch(int,int);

void main()
{
cout<<"ENTER SIZE OF THE ARRAY"<cin>>max;
cout<<"ENTER ELEMENTS IN ASCENDING ORDER"<for(i=l;icin>>arr[i];
cout<<"enter element to be searched"<cin>>se;
p=binsrch(l,max);
if(p==1)
{
cout<<"search is succesful\n";
cout<<"element is found at:" <}
else
cout<<"NOT SUCCESFUL\N";
getch();
}
int binsrch(int low,int high)
{
if(low==high)
{
if(arr[low]==se)
return 1;
else
return 0;
}
else
{
mid=(low+high)/2;
if(arr[mid]==se)
return 1;
else
{
if(se binsrch(low,mid-1);
else
binsrch(mid+1,high);
}
}
}




OUTPUT:

ENTER SIZE OF THE ARRAY
8
ENTER ELEMENTS IN ASCENDING ORDER
-2
0
7
8
23
45
56
78
enter element to be searched
56
search is succesful
element is found at:6

No comments: