#include <iostream>
using namespace std;
int search (int a[],int n,int s)
{
int mid,high,low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(s==a[mid])
{
return(mid);
}
else if(s>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
return -1;
}
}
int main()
{
int a[15],n,i,flag=0,s;
cout<<"Enter Number of elements";
cin>>n;
cout<<"Enter the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to be searched";
cin>>s;
flag=search(a,n,s);
if(flag!=-1)
cout<<"Search succesfull. Number found at "<<flag+1;
else
{
cout<<"Search unsuccesfull";
}
return 0;
}
Comments
Post a Comment