利用二分查找判断一个数组中是否存在某个数

#include <iostream>
#include <algorithm>
#include <map>
#include <string.h>
#include <string>
#include <list>
#include <stack>
#include <queue>
#include <cstdio>
#include <math.h>
using namespace std;

const int n = 5;

int a[n];

bool binary_search(int x){
	int l = 0, r = n;
	
	sort(a,a+n);
	
	while(r-l>=1){
		int mid = (r+l)/2;
		if(a[mid] == x){
			return true;
		}
		else if(a[mid] > x){
			r = mid;
		}
		else{
			l = mid + 1;
		}
	}
	
	return false;
}

int main() {

	ios::sync_with_stdio(false);

	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	
	int x;
	cin>>x;
	
	if(binary_search(x)){
		cout<<"Found..."<<endl;
	}
	else{
		cout<<"Not Found..."<<endl;
	}

	return 0;
}