简体   繁体   English

Java插入排序:“相同”变量得到不同的结果

[英]Java insertion sorting: "same" variable get different result

I was doing an insertion sort using Java.我正在使用 Java 进行插入排序。 For example, if I have an integer array {8,7,6,5,4,3,2,1},例如,如果我有一个整数数组 {8,7,6,5,4,3,2,1},

This will give me the result which is wrong: 7,6,5,4,3,2,1,8 picture这会给我错误的结果:7,6,5,4,3,2,1,8图片

public static int[] insertionSort(int[] list) {
    int[] insertionList = list.clone();
    for(int i = 1; i < insertionList.length; i++) {
        int temp = insertionList[i];
        int j = i - 1;
        while(j >= 0 && insertionList[j] > insertionList[i]) {
            insertionList[j + 1] = insertionList[j];
            j--;
        }
        insertionList[j + 1] = temp;
    }
    return insertionList;
}

And this will give me the result which I wanted: 1,2,3,4,5,6,7,8 picture这会给我我想要的结果:1,2,3,4,5,6,7,8图片

public static int[] insertionSort(int[] list) {
    int[] insertionList = list.clone();
    for(int i = 1; i < insertionList.length; i++) {
        int temp = insertionList[i];
        int j = i - 1;
        while(j >= 0 && insertionList[j] > temp) {
            insertionList[j + 1] = insertionList[j];
            j--;
        }
        insertionList[j + 1] = temp;
    }
    return insertionList;
}

Just wondering what different between insertionList[i] and temp .只是想知道insertionList[i]temp之间有什么不同。 I wrote two println statements to test these, but they also show the same number.我写了两个println语句来测试这些,但它们也显示相同的数字。

Thanks!!!!!谢谢!!!!!

The difference is that the 'insertionList' gets modified inside the while loop.不同之处在于'insertionList' 在while 循环中被修改。 When you set the temp variable to 'insertionList[i]' the value of temp remains the same throughout the while loop.当您将 temp 变量设置为 'insertionList[i]' 时,temp 的值在整个 while 循环中保持不变。

while(j >= 0 && insertionList[j] > insertionList[i]) {
            insertionList[j + 1] = insertionList[j];
            j--;
        }

the wrong is here.错误在这里。 insertionList[j + 1] = insertionList[j]插入列表[j + 1] = 插入列表[j]

in the first execution, j + 1 = i, and it will change the value of insertionList[i]在第一次执行时,j + 1 = i,它会改变insertList[i]的值

and it will effect to your condition.它会影响你的病情。

insertionList[j] > insertionList[i]

So it should be change to temp variable.所以应该改为临时变量。

while(j >= 0 && insertionList[j] > insertionList[i]) In this line you are comparing with the ith element of insertionList and during the iteration in while loop insertionList[j + 1] = insertionList[j]; while(j >= 0 && insertionList[j] > insertionList[i])在这一行中,您将与insertionList的第 i 个元素进行比较,并且在while循环中的迭代中insertionList[j + 1] = insertionList[j]; this code might change the value insertionList[i] was holding.此代码可能会更改insertionList[i]持有的值。 But when you are using temp variable then for the whole iteration the value for comparison here insertionList[j] > temp does not change.但是当您使用temp变量时,对于整个迭代,这里的比较值insertionList[j] > temp不会改变。 That's why you are getting different output.这就是为什么你得到不同的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM