标签:
#include <iostream>
using namespace std;
int binarySearch(int *a, int low, int high, int key)
{
int mid;
while(low <= high){
mid = (low + high)/2;
if(key > a[mid])
low = mid + 1;
else
if(key < a[mid])
high = mid - 1;
else
return mid;
}
return -1;
}
int main()
{
int a[] = {1,3,5,7,9,11};
cout << binarySearch(a, 0, 6, 3) << endl;
cout << binarySearch(a, 0, 6, 0) << endl;
cout << "Hello world!" << endl;
return 0;
}
标签:
原文地址:http://www.cnblogs.com/z-dianjun/p/5903190.html