概念:
适用范围:
有序数组
在顺序查找的基础上改进,每次查找都是用mid
时间
\(O(logn)\)
算法
int l = 0,r = n-1;
while(l<=r){//这里因为用来等号下面就要用减或者加
int mid = l + (r - l ) / 2;
if(arr[mid]>target){
r = mid - 1;
}else{
l = mid + 1;
}
}
//如果上面没等
int l = 0,r = n-1;
while(l<r){//这样当等的时候就会结束
int mid = l + (r - l ) / 2;
if(arr[mid]>target){
r = mid;
}else{
l = mid + 1;
}
}
标签:二分,int,mid,else,ltarget,while,查找
From: https://www.cnblogs.com/tsqo/p/17018142.html