C++二分查找算法演示源码

如下内容段是关于C++二分查找算法演示的内容。
#include <cstdio>
{
int l = 0, r = n-1;
int mid;
while (l <= r){
mid = (l + r) >> 1;
if (a[mid] == key)
return mid;
if (a[mid] > key)
r = mid - 1;
else
l = mid + 1;
}
return -1;
}
{
int l = 0, r = n-1;
while (l < r){
int mid = (l + r) >> 1;
if (a[mid] >= key)
r = mid;
else
l = mid + 1;
}
if (a[r] == key)
return r;
return -1;
}
{
int l = 0, r = n-1;
while (l < r){
int mid = (l + r + 1) >> 1;
if (a[mid] <= key)
l = mid;
else
r = mid - 1;
}
if (a[l] == key)
return l;
return -1;
}
{
int l = 0, r = n-1;
int mid;
while (l <= r){
mid = (l + r) >> 1;
if (mid > 0 && a[mid-1] > a[mid])
r = mid - 1;
else if (mid < n-1 && a[mid+1] > a[mid])
l = mid + 1;
else
return a[mid];
}
}

上一篇:httpClient中的三种超时设置小结


下一篇:UVA Getting in Line