标签:bin 开始 对比 复杂度 [] class 有序 顺序查找 code
最简单的从头开始对比查找。
int binarySearch(int arr[], int x, int len){
int left = 0, right = len-1;
while(left <= right){
int mid = (left + right) / 2;
if(x == arr[mid]){
return mid;
}
if(x > arr[mid]){
left = mid + 1;
}else{
right = mid -1;
}
}
return -1;
}
时间复杂度为O(1)
标签:bin 开始 对比 复杂度 [] class 有序 顺序查找 code
原文地址:https://www.cnblogs.com/pycrab/p/9879030.html