Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在我的插入排序算法中执行这个内部循环?_Java_Arrays_Conditional_Insertion Sort - Fatal编程技术网

Java 为什么在我的插入排序算法中执行这个内部循环?

Java 为什么在我的插入排序算法中执行这个内部循环?,java,arrays,conditional,insertion-sort,Java,Arrays,Conditional,Insertion Sort,我相信这个问题以前有人问过,但是在这里找到正确的答案总是很困难的 这是我的密码: public class InsertionSort2 { public static void main(String[] args) { int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 }; insertionSort(input); } private static void

我相信这个问题以前有人问过,但是在这里找到正确的答案总是很困难的

这是我的密码:

public class InsertionSort2 {

public static void main(String[] args) {                                
    int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
    insertionSort(input);
    }

private static void printNumbers(int[] input) {
    System.out.print("\n" + "THis is the sorted array: ");
    for (int i = 0; i < input.length; i++) {
    System.out.print(input[i] + ", ");
    System.out.println("\n");
    }

public static void insertionSort(int array[]) {
    int n = array.length;
    for (int j = 1; j < n; j++) { // int j is the index in the array that is being compared to the key
    int key = array[j]; // the key is the actual number of the array index j
    int i = j-1;  // int i is the index right before j (one less than j)
    System.out.print("\n" + "outer Loop j = " + j);
    System.out.print("\n" + "outer Loop i = " + i);
    System.out.print("\n" + "outer Loop key = " + key + "\n");
    while ( (i > -1) && ( array [i] > key ) ) { // this is where the comparison of the numbers takes place
        System.out.print("\n" + "inner Loop array [i] (this is the lower index in the array) = " + array [i]); // array [i] refers to the value of number at the position i.
        System.out.print("\n" + "inner Loop array [i+1] (this is the higher index in the array) = " + array [i+1]); // this is the value of the number one higher in the array
        array [i+1] = array [i]; // here the system puts the higher value into the lower value's spot
        i--;
        System.out.print("\n" + "inner Loop [i--] = " + i);
            }
    array[i+1] = key; // here the program changes the key to be the next item in the array so that it can go through the for loop again.            
  printNumbers(array);
    }
}
}
为什么内部
while
循环执行前三次?似乎没有满足执行while循环的条件


系统还打印与不应运行while循环的情况相反的内容。
array[i]
打印正确的数组值,而
array[i+1]
打印错误的数组值(一个较小而不是一个较大)。

我将解释其中一个多重打印:

Why am I being run? //key=0 array[i]=34 -> array[i]>key -> 2, 4, 6, 9, 12, 23, 34, 34, 1,
Why am I being run? //key=0 array[i]=23 -> array[i]>key -> 2, 4, 6, 9, 12, 23, 23, 34, 1,
Why am I being run? //key=0 array[i]=12 -> array[i]>key -> 2, 4, 6, 9, 12, 12, 34, 34, 1,
Why am I being run? //key=0 array[i]=9 -> array[i]>key -> 2, 4, 6, 9, 9, 23, 34, 34, 1,
Why am I being run? //key=0 array[i]=6 -> array[i]>key -> 2, 4, 6, 6, 12, 23, 34, 34, 1,
Why am I being run? //key=0 array[i]=4 -> array[i]>key -> 2, 4, 4, 9, 12, 23, 34, 34, 1,
Why am I being run? //key=0 array[i]=2 -> array[i]>key -> 2, 2, 6, 9, 12, 23, 34, 34, 1,
然后到达
i==-1
的情况并结束循环

0, 2, 4, 6, 9, 12, 23, 34, 1, 
希望这能帮助你理解这个算法是如何工作的

对于前3个案例:

第一:

所以

循环执行一次,然后得到
i==i--==1
,循环结束

第2例:

j == 2
i == j-1 == 1
key == 9
array[i] == 4
所以

循环无法运行

第三种情况:

j == 3
i == j-1 == 2
key == 6
array[i] == 9
所以


循环运行一次,然后得到
i==i--==1
array[i]=4
。因此,
array[i]您的代码与您的描述不匹配有几个方面。您需要学习使用调试器。插入排序中的内部循环用于向上移动值,直到元素处于正确位置。我不确定这是否是您真正想知道的,请澄清您的问题。@ElliottFrisch编辑,希望这有助于澄清问题。数组[I+1]的值没有任何错误,请解释您觉得有什么问题。我想问的是,为什么在不满足条件的情况下执行while循环。
((i>-1) && (array[i]>key)) == true
j == 2
i == j-1 == 1
key == 9
array[i] == 4
((i>-1) && (array[i]>key)) == false
j == 3
i == j-1 == 2
key == 6
array[i] == 9
((i>-1) && (array[i]>key)) == true