public class 递归之二分查找 {
//low是开始下标,high是结束下标,key是要查找的值
public static int binarySearch1(int[] arr,int low,int high,int key) {
//递归出口
if(low>high) {
return -1;
}
int mid = low+((high-low)>>1);
int midVal = arr[mid];
if(midVal<key) {
return binarySearch1(arr,mid+1,high,key);
}
else if(midVal>key) {
return binarySearch1(arr,low,mid-1,key);
}
else
return mid;
}
}
递归之二分查找
最新推荐文章于 2024-10-30 22:30:35 发布