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);
}
}
|
|