在使用递归编写二分查找代码时,我遇到了一些疑惑。

huangapple go评论74阅读模式
英文:

while coding for binary search using recursion i faced some doubts

问题

当只使用**if**我必须返回一些整数

public class solution {
    
    public static int binarySearch(int arr[], int x, int si, int ei) {
        if (si > ei) {
            return -1;
        }
        int mid = (si + ei) / 2;
        if (arr[mid] == x) {
            return mid;
        }
        if (arr[mid] > x) {
            return binarySearch(arr, x, si, mid - 1);
        }
        if (arr[mid] < x) {
            return binarySearch(arr, x, mid + 1, ei);
        }
        return 0;
    }
}

但是当使用**if-else-if**我就不需要返回任何整数为什么

public class solution {
    
    public static int binarySearch(int arr[], int x, int si, int ei) {
        if (si > ei) {
            return -1;
        }
       
        int mid = (si + ei) / 2;
        if (arr[mid] == x) {
            return mid;
        } else if (arr[mid] > x) {
            return binarySearch(arr, x, si, mid - 1);
        } else {
            return binarySearch(arr, x, mid + 1, ei);
        }
    }
}
英文:

When using only if, I had to return some integer

public class solution {
public static int binarySearch(int arr[], int x,int si,int ei){
if(si&gt;ei){
return -1;
}
int mid=(si+ei)/2;
if(arr[mid]==x){
return mid;
}
if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
if(arr[mid]&lt;x){
return binarySearch(arr,x,mid+1,ei);
}
return 0;
}
}

but when using if-else-if, I don't have to return any integer, why?

public class solution {
public static int binarySearch(int arr[], int x,int si,int ei){
if(si&gt;ei){
return -1;
}
int mid=(si+ei)/2;
if(arr[mid]==x){
return mid;
}
else if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
else {
return binarySearch(arr,x,mid+1,ei);
}
}
}

答案1

得分: 3

当一个方法在签名中有返回类型时,无论在什么条件下,该方法都应该返回一些内容。在Java中,这个检查是在编译时进行的。

当你在代码中使用 if 时,逻辑上条件可以为真或假。如果条件为真,方法将从 if 代码块中返回某些内容。但如果条件为假,方法将不会有任何东西返回(因为 if 条件内的代码不会执行)。因此,在这种情况下,方法需要在 if 条件结果为假时返回一些默认值。从第一个方法开始,如果所有 if 条件都像这样:

if(si > ei){
    return -1;
}
int mid = (si + ei) / 2;
if(arr[mid] == x){
    return mid;
}
if(arr[mid] > x){
    return binarySearch(arr, x, si, mid - 1);
}
if(arr[mid] < x){
    return binarySearch(arr, x, mid + 1, ei);
}

都为假,方法将不会处于返回任何内容的情况。

另一方面,当你在 if 后面使用 else(无论是 if-else 还是 if-elseIf-else),那么如果 if 为假(或 elseIf 为假),则 else 部分将从方法中返回某些内容。因此,总是会有某些内容可以从方法中返回。在第二个方法中,像这样:

if(arr[mid] == x){
    return mid;
}
else if(arr[mid] > x){
    return binarySearch(arr, x, si, mid - 1);
}
else
    return binarySearch(arr, x, mid + 1, ei);

如果第一个 if 条件为真,将返回 mid。如果 arr[mid] > x 为真,将返回 binarySearch(arr, x, si, mid - 1); 的结果。如果两者都为真,else 部分始终会有内容返回(在你的情况下是 binarySearch(arr, x, mid + 1, ei);)。

英文:

When a method has return type in signature, something should be returned from the method in all condition. And that check is made at compile time in Java.

When you use if in your code logically that condition can be true or false. If that's true the method will get something returned from if block. But if the condition is false, method won't get anything to return back (because code inside if condition in not executed). So in that case method need something to return as default when if condition is resulted as false. From first method if all if conditions like

        if(si&gt;ei){
return -1;
}
int mid=(si+ei)/2;
if(arr[mid]==x){
return mid;
}
if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
if(arr[mid]&lt;x){
return binarySearch(arr,x,mid+1,ei);
}

are false. Method won't be in situation to return anything.

On the other place when you use else with if (weather it is if-else or if-elseIf-else) then condition when if is false (or elseIf is false), the else part will return something from method. So always there will be something to return from method. In second method like

if(arr[mid]==x){
return mid;
}
else if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
else
return binarySearch(arr,x,mid+1,ei);
}

if the first if condition is true mid will be returned. If arr[mid]&gt;x is true binarySearch(arr,x,si,mid-1); result will be returned. If both are true else will always be there to return something (in your case binarySearch(arr,x,mid+1,ei);).

答案2

得分: 3

编译器无法猜测最后的if条件将始终为真。因此,你必须在条件为假的情况下提供一个返回值。尽管这永远不会发生。你甚至可以摆脱最后的if语句。

public class solution {

    public static int binarySearch(int arr[], int x, int si, int ei) {
        if (si > ei) {
            return -1;
        }
        int mid = (si + ei) / 2;
        if (arr[mid] == x) {
            return mid;
        }
        if (arr[mid] > x) {
            return binarySearch(arr, x, si, mid - 1);
        }
        return binarySearch(arr, x, mid + 1, ei);
    }

}
英文:

The compiler can not guess that the last if condition will always be true. So you have to provide a return value in case it is false. Even though it will never happen. You could even get rid of the last if statement.

    public class solution {
public static int binarySearch(int arr[], int x,int si,int ei){
if(si&gt;ei){
return -1;
}
int mid=(si+ei)/2;
if(arr[mid]==x){
return mid;
}
if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
return binarySearch(arr,x,mid+1,ei);
}
}

答案3

得分: 1

因为你的第二种方法的所有可能结果都会返回某些东西。你总是需要确保当你的方法不是 void 时。

在第一种情况下,你需要为当你的三个 if 语句的条件都不成立时指定返回值。

if (arr[mid] == x) {
    return mid;
}
if (arr[mid] > x) {
    return binarySearch(arr, x, si, mid - 1);
}
if (arr[mid] < x) {
    return binarySearch(arr, x, mid + 1, ei);
}

然而,很明显你永远不会面临所有上述条件都为 false 的情况,但编译器无法判断这一点,因此你需要设计你的代码,使编译器知道在所有可能的情况下你的方法都会返回值。因此,第二种实现更加“干净”和逻辑上正确。

英文:

Because all possible outcomes of your second method's work will return something. You always need to ensure that when your method is not void.

In first case you need to specify return value for the case, when none of your three if statements' conditions is true.

    if(arr[mid]==x){
return mid;
}
if(arr[mid]&gt;x){
return binarySearch(arr,x,si,mid-1);
}
if(arr[mid]&lt;x){
return binarySearch(arr,x,mid+1,ei);
} 

However, it is clear to see that you will never face a situation, when all the above conditions are false, but compiler cannot figure this out, so you need to design your code that way, in which compiler will know that in all possible conditions your method will return value. So the second implementation is more "clean" and logically correct

答案4

得分: 0

你已将返回类型设置为int。在使用if语句时,不能确定其中一个if语句一定会执行并返回值。

在上面的代码中,如果条件不满足,两个if都可能不会执行,因此可能不会得到返回值。

而在else if和else情况下,我们可以确定其中一个情况一定会执行并返回值,百分之百的确定性。

在上面的代码中,只有一个if条件会执行,我们一定会得到返回值。

英文:

you have set the return type to int. When you are using if statements, there is no surety that one of the if statements has to work and you will get the return value.

if()
{
return value;
}
if()
{
return value;
}

In the above code both ifs can work or both can not work if conditions are not satisfied. therefore it might be possible that we will not get a return value.

and in else if, else case we have surety that one of the case will definitely work and we will get a return value 100%

if()
{
return value;
}
else if()
{
return value;
}
else{
return value;
}

in the above code only one of the if condition will work and we will always get a return value.

huangapple
  • 本文由 发表于 2020年8月6日 17:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63280280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定