#include <iostream>
using namespace std;
//折半查找法,适合于在已经排好了序(一般小到大)的数中查找.
//算法思路,二分查找,每次与中间的比较,来减小一般的查找范围
int half_search(int *ary,int n,int key)
{
int low,high,i,middle;
low=1;
high=n;
while(low!=high)
{
if(key==ary[low])
return low;
if(key==ary[high])
return high;
middle=(low+high)/2;
if(key<=ary[middle])
high=middle;
else
low=middle;
}
if(low==high)
{
if(ary[low]==key)
return low;
else
return 0;
}
}
int main()
{
int ary[11]={0,1,16,24,35,47,59,62,73,88,99};
cout<<half_search(ary,10,62)<<endl;
system("pause");
return 1;
}