英文:
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 <= high) {
int mid = low + high >>> 1;
long midVal = a[mid];
if (midVal < key) {
low = mid + 1;
} else {
if (midVal <= key) { // Why here have we used '<=' rather than '=='
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
语句中更改比较,并交换该语句的then
和else
分支:
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 <= 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.
}
Now if you look at the if
statements:
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
you could rewrite it as (still the same code as before):
if (midVal < key) {
low = mid + 1;
} else {
if (midVal > 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 < key) {
low = mid + 1;
} else {
if (midVal <= key) {
return mid; // key found
}
high = mid - 1;
}
This code is functionally equivalent but the intention is no longer visible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论