有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在这种插入排序中,数组是如何排序的?我不明白算法为什么不能使整个数组的数字相同

public static void insertionSort (int [] array)
{
    int shift, iterate, lock;

    for (iterate = 1; iterate < array.length; iterate++)
    {
        lock = array [iterate];
        shift = iterate - 1;

        // I don't understand this while loop. Wouldn't the whole array 
        // be 1 number afterwards?

        while (shift >= 0 && array [shift] > lock)
        {
            array [shift + 1] = array [shift];
            shift = shift - 1;
        }
        array [shift + 1] = lock;
    }
}

共 (0) 个答案