在Java中实现的二分搜索中使用的等号和不等号运算符。

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

Equality and in-equality operators used in binary search implemented in Java

问题

    private static int binarySearch0(long[] a, int fromIndex, int toIndex, long key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = low + high >>> 1;
            long midVal = a[mid];
            if (midVal < key) {
                low = mid + 1;
            } else {
                if (midVal <= key) {
                    return mid;
                }

                high = mid - 1;
            }
        }

        return -(low + 1);
    }
英文:

I have been trying to figure out why in the binary search code of Java, we have used '<=' rather than simply using a '=='. Is it some sort of optimization?

The following piece of code is from Class: java.util.Arrays, method: binarySearch0()

Code:

    private static int binarySearch0(long[] a, int fromIndex, int toIndex, long key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while(low &lt;= high) {
            int mid = low + high &gt;&gt;&gt; 1;
            long midVal = a[mid];
            if (midVal &lt; key) {
                low = mid + 1;
            } else {
                if (midVal &lt;= key) { // Why here have we used &#39;&lt;=&#39; rather than &#39;==&#39;
                    return mid;
                }

                high = mid - 1;
            }
        }

        return -(low + 1);
    }

答案1

得分: 0

你也可以使用'=='。如果 midVal < key,它将永远不会进入 else 部分。

英文:

You can use '==' too. As if midVal < key, it will never go to the else part.

答案2

得分: 0

您的代码与JDK内部使用的代码不同(http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/be44bff34df4/src/share/classes/java/util/Arrays.java#l1825),因此我只能假设您使用了某种反编译工具来生成您的源代码。

原始源代码易读且清晰地传达了意图:

private static int binarySearch0(long[] a, int fromIndex, int toIndex,
                                 long key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        long midVal = a[mid];

        if (midVal < key)
            low = mid + 1;
        else if (midVal > key)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1);  // key not found.
}

现在,如果您看一下if语句:

if (midVal < key)
    low = mid + 1;
else if (midVal > key)
    high = mid - 1;
else
    return mid; // key found

您可以将其重写为(仍然与之前的代码相同):

if (midVal < key) {
    low = mid + 1;
} else  {
    if (midVal > key)
        high = mid - 1;
    else
       return mid; // key found
}

现在,您可以在第二个if语句中更改比较,并交换该语句的thenelse分支:

if (midVal < key) {
    low = mid + 1;
} else  {
    if (midVal <= key) {
        return mid; // key found
    }
    high = mid - 1;
}

这段代码在功能上等效,但意图不再明显可见。

英文:

Your code differs from the one used within the JDK (http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/be44bff34df4/src/share/classes/java/util/Arrays.java#l1825), so I can only assume that you used some kind of decompiler to arrive at your source code.

The original source code is readable and clearly communicates the intention:

private static int binarySearch0(long[] a, int fromIndex, int toIndex,
                                 long key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low &lt;= high) {
        int mid = (low + high) &gt;&gt;&gt; 1;
        long midVal = a[mid];

        if (midVal &lt; key)
            low = mid + 1;
        else if (midVal &gt; key)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1);  // key not found.
}

Now if you look at the if statements:

        if (midVal &lt; key)
            low = mid + 1;
        else if (midVal &gt; key)
            high = mid - 1;
        else
            return mid; // key found

you could rewrite it as (still the same code as before):

        if (midVal &lt; key) {
            low = mid + 1;
        } else  {
            if (midVal &gt; key)
                high = mid - 1;
            else
               return mid; // key found
        }

Now you can change the comparison in the second if and swap the then and else branches of that statement:

        if (midVal &lt; key) {
            low = mid + 1;
        } else  {
            if (midVal &lt;= key) {
                return mid; // key found
            }
            high = mid - 1;
        }

This code is functionally equivalent but the intention is no longer visible.

huangapple
  • 本文由 发表于 2020年7月23日 20:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63054140.html
匿名

发表评论

匿名网友

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

确定