- #include<iostream>
- using namespace std;
- int binary_search(int arr[],int n,int e)
- {
- int low = 0;
- int high = n-1;
- while(low<=high)
- {
- int mid = low + ((high - low)>>2);
- if(arr[mid]>e)
- high = mid -1;
- else if(arr[mid]<e)
- low = mid + 1;
- else
- return mid;
- }
- return -1;
- }
- int main()
- {
- int array[4] = {10,12,14,16};
- cout<<binary_search(array,4,16)<<endl; //数组,个数,待查找数据
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1612201411288.html
来源: http://www.codesnippet.cn/detail/1612201411288.html