Binary Search STL

I am executing program by using binary_search function for the array with element [1,5,6,4,8] .The code is working fine for all elements but showing wrong result for element 4.
code for refrence :-
#include
#include
using namespace std;

int main() {

int arr[] = {1,5,6,4,8};

int n = sizeof(arr)/sizeof(int);

int key;
cin>>key;

bool found = binary_search(arr,arr+n,key);

if(found)
   {
   cout<<"Element found";
       
   }
else{

  cout<<"Not found";
    
}

}

hello @ankitkaushal please wait i have to test the code on ide.

hello @ankitkaushal you have made one error in your array.
binary search has one condition that the array must be sorted otherwise it will throw this error only.


if you have any doubt you can ask here:
Happy Learning !!

Yes sir I got it. I thought binary_search() function itself sorts out the array.

@ankitkaushal if you feel that your doubt is cleared please mark this doubt as resolved.

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.