I'm trying to write a recursive binary search function using below approach. I'm basically using divide and conquer strategy and everything looks good to me in code, but unable to figure out where my code and approach goes wrong.
#include<iostream>
using namespace std;
bool b_search(int *arr, int n, int start, int end){
if(start==end){
if(arr[start]==n){
return true;
}
else{
return false;
}
}
else{
int mid=start+(end-start)/2;
if(arr[mid]==n){
return true;
}
else if(arr[mid]>n){
return b_search(arr,n,mid+1,end);
}
else{
return b_search(arr,n,start,mid-1);
}
}
}
int main(){
int arr[8]={3,5,8,11,13,15,16,25};
cout<<b_search(arr,16,0,7);
}
I'm getting output as zero but it should be 1.