I have tried written same code as in tutorial....But it's output is wrong Pls tell me my mistake

#include

using namespace std;

int search(int a[],int s,int e,int key)

{

int mid=(s+e)/2;
if(a[mid] == key)
return mid;
if(a[s]<=a[mid])
{
    if((key>=a[s])&&(key<=a[mid]))
    return (a,s,mid-1,key);
    else return (a,mid+1,e,key);
}
 
    if((key>=a[s]) && (key<=a[e]))
     return (a,mid+1,e,key);
     else return (a,s,mid-1,key);

}

int main()

{

int a;
cin>>a;
int arr[a];
for(int i=0;i<a;i++)
cin>>arr[i];
cout<<"Enter Key :";
int key;
cin>>key;
cout<<"At index "<<search(arr,0,a-1,key)<<endl;

}

@Lovishbansal
Hello Lovish,
replace if statement

with
if(key>=a[mid] && key<=a[e])

it will work

@Lovishbansal
Hello Lovish,
I am closing this thread ,for any doubt use your newly created thread.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.