java 实现二分查找
程序员文章站
2024-03-20 17:32:46
...
//数组的二分查找
public int find(int findKey) {
int mix=0;
int max=size-1;
while(true) {
int mid=(mix+max)/2;
if(findKey==arr[mid]) {
return mid;
}else if(mix>max) {
return -1;
}else{
if(findKey>arr[mid]) {
mix=mid+1;
}else if(findKey<arr[mid]){
max=mid-1;
}
}
}
}