Binary Search ~ Program to search a value from array using Binary search

#include<iostream.h>
#include<conio.h>

void binary_search(int *A,int value,int s,int e);

void display(int *arr,int count)
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void binary_search(int *A,int value,int s,int e)
{
int mid=0;
mid=(s+e)/2;
if(value==A[mid])
{ cout<<"\nValue found\n"; }
else if(value>A[mid])
{ s=mid+1;
 binary_search(A,value,s,e);
}
else if(value<A[mid])
{
e=mid-1;
binary_search(A,value,s,e);
}
else
{ cout<<"not found";}
}

void main()
{
clrscr();
int A[]={5,4,9,7,8,1,3,2,6},value=0;
cout<<"\nEnter Value You Want to Search: ";
cin>>value;
binary_search(A,value,0,9);
cout<<"\n\nArray is following:\n";
display(A,9);

getch();
}


No comments

Powered by Blogger.