Could someone tell me what I am doing wrong? My recursive call is not terminating when it sees the "return mid" but it continues to execute the remaining conditions.
public class BinSearch {
public static int binSearch(int[] a, int key, int low, int high){
if (low >= high){
System.out.println("Low = High");
return -1;
}
int mid = low + ((high - low)/2);
//System.out.println("Value of Mid for low:"+low+", high:"+high+" is:"+mid);
if (a[mid] == key){
System.out.println("Value of mid is:"+a[mid]);
return mid;
}
else if (a[mid] < key){
binSearch(a, key, mid+1, high);
}
else {
binSearch(a, key, low, high)
}
System.out.println("I am somehow out of the loop!");
return -1;
}
public static void main (String[] args){
System.out.println("Hello World");
int[] arr = {1, 6, 9, 13, 22, 27, 29 , 38, 49, 50, 61, 72};
int key = 22;
int high = arr.length;
int response = binSearch(arr, key, 0, high);
System.out.println("The response is"+ response);
}
}