[英]I am mindboggled. Insertion Sort (basic sort, i know) algorithm is doing something I can't explain
Ok, hear me out.好的,听我说完。 Here is my code for insertion sort.
这是我的插入排序代码。
for (int i = 0; i < arr.length; i++) {
T curr = arr[i];
int i2 = i - 1;
// if (i2 == -1) {
// System.out.println("yes");
// break;
// }
while (i2 >= 0 && comparator.compare(arr[i2], curr) > 0) {
arr[i2 + 1] = arr[i2];
i2--;
}
arr[i2 + 1] = curr;
}
So, as of right now, I have所以,截至目前,我有
if (i2 == -1) {
sout("yes");
break;
}
because I wanted to skip the iteration where i2 will obviously be equal to -1 and thus not have a single effect on the array because the first iteration does nothing and keeps the first element in the 0 position of the array because that is exactly how insertion sort works.因为我想跳过 i2 显然等于 -1 的迭代,因此对数组没有任何影响,因为第一次迭代什么都不做,并将第一个元素保持在数组的 0 位置,因为这正是插入的方式排序工作。
Now, I'm not exactly sure what's going on, because, as this aforementioned if statement is commented out, the algorithm works exactly as expected.现在,我不确定发生了什么,因为正如前面提到的 if 语句被注释掉了,算法完全按预期工作。 BUT , when I un comment it, the algorithm fails.
但是,当我取消评论时,算法失败了。
What I don't understand is how commenting it out, or not including this if statement at all, leads to a failing algorithm, because no matter what, when i2 == -1, there is literally, as far as I can tell, no effect on the algorithm, since, when i2 is -1, and the if statement described is omitted, the while loop does not execute, and the arr[i2 + 1] = curr statement DOES execute, but it will just keep the first element in its place, EXACTLY how the presence of the if statement keeps the first element in its place because it breaks the loop and does not alter a single thing.我不明白的是,如何将其注释掉,或者根本不包括这个 if 语句,会导致算法失败,因为无论如何,当 i2 == -1 时,据我所知,字面意思是,对算法没有影响,因为当 i2 为 -1 并且省略了所描述的 if 语句时,while 循环不会执行,并且 arr[i2 + 1] = curr 语句会执行,但它只会保留第一个元素在它的位置,确切地说 if 语句的存在如何将第一个元素保持在它的位置,因为它打破了循环并且不会改变任何事情。
I'm not sure if I'm absolutely insane and missing something inexplicably clear to a sane person, but I literally see not a single way that the omission or inclusion of the if statement described changes anything about the code, especially since i2 will only be -1 on the first iteration of the for loop.我不确定我是否绝对疯了,并且错过了一个理智的人无法解释的东西,但我确实没有看到遗漏或包含所描述的 if 语句改变任何关于代码的任何方式,特别是因为 i2 只会在 for 循环的第一次迭代中为 -1。
Thank you very much.非常感谢。
You should replace break
with continue
.您应该将
break
替换为continue
。
Because, at the first iteration, it will always be -1
and it will break the for loop, which means there are no further iterations.因为,在第一次迭代时,它总是
-1
并且会破坏 for 循环,这意味着没有进一步的迭代。
continue
is the right weapon. continue
是正确的武器。
if (i2 == -1) {
sout("yes");
continue;
}
The above code will skip the 1st iteration, while your code will terminate the for loop.上面的代码将跳过第一次迭代,而您的代码将终止 for 循环。
But, there is no need of this if
block because you are already filtering out 1st iteration in the condition of while
loop.但是,不需要这个
if
块,因为您已经在while
循环的条件下过滤掉了第一次迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.