如下内容段是关于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];
}
}
相关文章
- 03-14C++二分查找算法演示源码
- 03-14python中两种方法实现二分法查找,细致分析二分法查找算法
- 03-14南理第八届校赛同步赛-F sequence//贪心算法&二分查找优化
- 03-14二分查找算法(Python版)
- 03-14c++ stl二分查找与lower_bound
- 03-14浅谈二分查找框架+剑指 Offer 53 - I (C++)
- 03-14查找算法之二分查找
- 03-14算法图解: 1.二分查找
- 03-14python 二分查找算法实现
- 03-14二分查找算法的Python实现