有序表查找--二分法查找算法 c++实现
程序员文章站
2024-03-17 14:43:16
...
二分法有序表查找:
int binary_search (vector<int>vec ,int key,int low, int high)
{
int mid;
while (low <= high)
{
mid = (low + high )/ 2;
if (key < vec[mid])
high = mid - 1;
else if (key > vec[mid])
low = mid + 1;
else // key > vec[mid]
return mid ;
}
cout << "no find object value " << key << ", between " << low << " to " << high << endl;
return 0;
}
上一篇: 二分法在循环有序数组查找元素
下一篇: 牛客网机试题:全排列