二分查找比较简单:
template<class T>
int binary_search(T array[],int len, T value)
{
int left = 0, right = len -1;
while(left <= right)
{
int index = (left + right)/2;
if(value == array[index])
{
return index;
}
else if(value > array[index])
{
left = index + 1;
}
else if(value < array[index])
{
right = index - 1;
}
}
return -1;
}
也可以通过递归实现