public class recursion{
public static int recursionBinarySearch(int[] array, int first, int last, int target){
int midpoint;
//if the first number is greater than the last, the target number is not in the list
if (first > last){
System.out.println(-1);
return -1;
}
else{
midpoint = (first+last)/2;
//take the upper bound if number is greater than midpoint
if (array[midpoint] < target){
return recursionBinarySearch(array, midpoint+1, last, target);
}
// take the lower bound if the number is lesser than midpoint
if (array[midpoint] > target){
return recursionBinarySearch(array, first,midpoint-1, target);
}
System.out.println("index of target: " + midpoint);
return midpoint;
}
}
public static void main(String[] args){
// test array in main
int[] test_array = new int[]{ 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40 };
recursion.recursionBinarySearch(test_array, 0, test_array.length, 24);
}
}
recursion.main(null);