黑马程序员技术交流社区
标题:
二分查询
[打印本页]
作者:
lwr
时间:
2019-10-10 13:52
标题:
二分查询
package lwr;
public class recursionBinarySearch {
public static int recursionBinarySearch(int[] arr,int key,int low,int high){
if(key>arr[high]||key<arr[low]||low>high){
return -1;
}
int middle = (low+high)/2;
if(key<arr[middle]){
return recursionBinarySearch(arr,key,low,middle-1);
}else if(key>arr[middle]){
return recursionBinarySearch(arr,key,middle+1,high);
}else{
return middle;
}
}
public static void main(String[] args) {
int[] arr = {1,27,32,35,42,56,57,89};
int middle=recursionBinarySearch(arr,42,0,7);
System.out.println(middle);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2