简体   繁体   English

我难以置信。 插入排序(基本排序,我知道)算法正在做一些我无法解释的事情

[英]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.

相关问题 每当我在 java 中运行插入排序程序时,都会发生异常.. 但我不知道为什么 - Whenever I run the insertion sort program in java, exception occurs .. but i don't know why 导致无限循环的Java Shell排序算法:我在做什么错? - Java Shell Sort Algorithm Causing Infinite Loop: What Am I doing Wrong? 带插入的Quicksort排序完成-我在哪里出错? - Quicksort with insertion Sort finish - where am I going wrong? 此插入排序算法仅交换前 2 个元素并停止。 我该如何解决? - This insertion sort algorithm only swaps the first 2 elements and stops. How can I fix it? 我如何在java中运行这个插入排序代码? - how i can run this insertion sort code in java? 如何纠正选择和插入排序算法 - How can I rectify my selection and insertion sort algorithms 为什么我不想在插入类型中嵌入此循环? - Why I don't want to embed this loop in my insertion sort? 如何将移位元素功能与插入排序相结合? - How can I combine shift element function to insertion sort? 有一种有效的“合并排序”功能,但不是完全可以,有人可以告诉我我做错了什么吗? - Have a sort of working Merge Sort, but not quite, could someone tell me what I am doing wrong? 为什么&#39;i&#39;的值打印3次,在插入排序算法中它应该只打印1次 - Why value of 'i' is printing 3 times, it should only print 1 time in insertion sort algorithm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM