简体   繁体   中英

Why is return executed twice

In this case the value does match and the value of boolean is set to true,however return is called twice and does update the value to false.Can anyone suggest what am I missing here?.

public class BinSearch {

    public static void main(String[] args) {

        BinSearch bin=new BinSearch();
        int arr[]= {2,4,6,8,10,12,14,16};
        boolean b=bin.binSearch(arr,0,arr.length-1,12);
        System.out.println("Number found "+b);

    }

    public  boolean binSearch(int arr[],int low,int high,int val)
    {
        int mid=(low+high)/2;

        if(arr[mid]==val)
        {
            return true;
        }

        else if(arr[mid]>val)
        {
            binSearch(arr,mid+1,high,val);
        }

        else  
        {
            binSearch(arr,low+1,mid,val);
        }

        return false;
    }
}

You're missing two returns when calling the recursion:

return binSearch(...);

If you don't write them, the method will ignore the result of the recursion and just return false at the end. After doing that, the last return will be unnecessary and should be deleted. Finally, you need to check the case when low > high , that means that the element was not found.

Because your return false; overrides everything except for a case when the value is found on the first run and no recursive call is invoked. Please return for each recursive call. Additionally, you must check if your low boundary is less or equal to the high boundary. So, your code could be as follows:

public boolean binSearch(int arr[],int low,int high,int val)
{
    if (low <= high) {
       int mid=(low+high)/2;
       if(arr[mid]==val) {
          return true;
       } else if(arr[mid]>val) {
          return binSearch(arr,mid+1,high,val);
       } else {
          return binSearch(arr,low+1,mid,val);
       }
    }
    return false;
}

You can also try the following code in binSearch function. The only change made is that there is a boolean type variable capturing the value returned in the recursive calls and finally returning this varaible.

public  boolean binSearch(int arr[],int low,int high,int val) {
    int mid=(low+high)/2;
    boolean returnValue = false;

    if(arr[mid]==val) {
        return true;
    } else if(arr[mid]>val) {
        returnValue  = binSearch(arr,mid+1,high,val);
    } else {
        returnValue  = binSearch(arr,low+1,mid,val);
    }    
    return returnValue;
}
public class BinSearch {

    public static void main(String[] args) {

        BinSearch bin=new BinSearch();
        int arr[]= {2,4,6,8,10,12,14,16};
        boolean b=bin.binSearch(arr,0,arr.length-1,16);
        System.out.println("Number found "+b);

    }

    public  boolean binSearch(int arr[],int low,int high,int val)
    {
        int mid=(low+high)/2;
        //boolean b = false;
        while(low<=high)
        {
            if(arr[mid]==val)
            {
                return true;
            }

            else if(val>arr[mid])
            {
                return binSearch(arr,mid+1,high,val);
            }

            else  
            {
                return binSearch(arr,low+1,mid,val);
            }

        }

        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM