#include<iostream>
using namespace std;
int main()
{
int *a,n,no,mid,low=0,high,flag=0;
cin>>n;
high=n-1;
a=new int[n];
///getting array elements
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"enter the element to be searched";
cin>>no;
///binary search iteratively
while(low<=high){
mid=(low+high)/2;
if(a[mid]==no){
flag=1;
break;
}
else if(no>a[mid]){
low=mid+1;
}
else if(no<a[mid]){
high=mid-1;
}
}
if(flag){
cout<<"FOUND\n";
}
else{
cout<<"NOT FOUND\n";
}
return 0;
}
Comments
Post a Comment