0

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

    }
}

1 Answers1

0

It looks like you get the value of mid but then it is not saved and not used, so everytime it gets to the end of binSearch and outputs -1. You might want to make binSearch a helper function for the binary search, and instead of recursively calling binSearch, you could return binSearch(a, key, low, high). Also, intead of returning binSearch(a, key, low, high), you might want to return the lower half of the array by returning binSearch(a, key, low, mid-1). The function should return -1 when low is greater than high. Your code looks good and hope that helps!

public class BinSearch {

public static int Search(int[] a, int key){
  return binSearch(a, key, 0, a.length-1);
}

public static int binSearch(int[] a, int key, int low, int high){        //make this the helper function
    
    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){
      //add return
        return binSearch(a, key, mid+1, high);
    }
    else {
      //add return and return for lower part of array
      return binSearch(a, key, low, mid-1);
        //return 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 = Search(arr, key);
  //binSearch(arr, key, 0, high);
    System.out.println("The response is"+ response);
}

}

Ferret
  • 1
  • 1