Recursive Code For Binary Search Not Working (Recursion - I Basics Video 5)

Below is the code I wrote for recursive binary Search but I couldn’t understand why it is returning key not found as output everytime and only working if my key is middle element (here 4 ) of the array
#include
using namespace std;
void BinarySearch(int a[],int s,int n,int key){
// base case
if(n == 0){
cout<<“key not found”<<endl;
return;
}
int e = n - 1;
int mid = (s + e)/2;
if(key > a[mid]){
BinarySearch(a,mid+1,n-1,key);
}
if(key < a[mid]){
BinarySearch(a,0,mid-1,key);
}
if(key == a[mid]){ // Trust that function will find the key in array
cout<<mid;
}
}
int main() {
int arr[] = {1,2,3,4,5,10,20};
int n = sizeof(arr)/sizeof(int);
int key = 10;
BinarySearch(arr,0,n,key);
return 0;
}

hi @manchandagitansh4_b5d09b65db5085fd,
send the code on ide.codingblocks.com

  1. write
  2. save
  3. send the url of the code saved

hi @manchandagitansh4_b5d09b65db5085fd
refer