public class Q二分查找递归 {
//查找有序的从小到大的数组
//返回下标
public static int f(int[] arr,int right,int left,int key){
if(right>left){
return -1;
}
int mid=(right+left)/2;
if(key<arr[mid]){
return f(arr,right,mid,key);
}else if(key>arr[mid]){
return f(arr,mid+1,left,key);
}else{
return mid;
}
}
public static void main(String[] args) {
int[] nums={1,2,3,4,5,6,7,8,9,10};
System.out.println(f(nums,0, nums.length-1,7));
}
}