【问题标题】:Insertion sort Java not properly sorting插入排序Java没有正确排序
【发布时间】:2021-01-09 03:15:44
【问题描述】:

我正在尝试编写冒泡排序、选择排序和插入排序 MYSELF。但是,我在插入排序时遇到了麻烦。我将提供我的代码以及每一行的作用

public void sortMethod() {
    int count = 1;
    for (int j = 0; j < Unsorted.length - 1; j++) {
        int index = 0;
        if (Unsorted[count] < Unsorted[count - 1]) {
            int temp = Unsorted[count];
            for (int i = count; i > 0; i--) {
                if (Unsorted[i] > Unsorted[count]) {
                    index = i;
                    Unsorted[i] = Unsorted[i - 1];
                }
            }
            Unsorted[index] = Unsorted[count];
        }
        count = count + 1;
    }
}

好的,所以 int count 是要找出排序数组从哪里开始。然后我声明 index 以查找在排序数组之后放置元素的位置,并为未排序数组的第一个元素声明一个临时 int,如果它小于排序数组的最后一个元素。然后它反转数组直到第一个元素,如果它大于我添加的元素,则将索引分配给它的索引。从本质上讲,我知道把它放在哪里。然后 unsorted[I] = unsorted[I - 1] 将已排序数组从未排序数组的第一个元素所属的位置移动。然后将未排序数组的第一个元素分配给它所属的位置。每次增加计数

Array: 31 27 45 23 22 3 1 13 1 42 
Array after sort: 27 1 1 3 22 23 1 1 1 42 
BUILD SUCCESSFUL (total time: 0 seconds)

【问题讨论】:

标签: java


【解决方案1】:

这是我必须更改您的代码以使其进行插入排序的最小量,因为我理解这种排序可以工作。请注意,您使用的整数值比必要的要多,并且其中有一个额外的 if 是您不需要的。我放了很多 cmets 来反映我在修复它时的想法,因此您可以希望了解代码在做什么:

public class Test
    private static int[] Unsorted = {444, 7, 22, 4, 3, 2, 1, -34, -999};

    public static void sortMethod() {

        // We'll consider each position in the array in order.  For each round,
        // 'j' is pointing at the last item in the part of the array that we know is
        // correctly sorted.  The first item after 'j' is the next candidate.  We
        // want to INSERT it into the right place in the part of the array that
        // is already sorted. NOTE: The first item is never a candidate because
        // an array with one element is always sorted.
        for (int j = 0; j < Unsorted.length - 1; j++) {
            // save off next candidate value, the first value that may be out of order
            int temp = Unsorted[j + 1];

            // Move all the items in the sorted part of the array that are greater
            // than the candidate up one spot.  We know we have room to shift up
            // because we've saved off the value at the candiate position.
            int i = j;
            while (i >= 0 && Unsorted[i] > temp) {
                Unsorted[i + 1] = Unsorted[i];
                i = i - 1;
            }
            // Put the candidate in the hole that is left.  This inserts it such
            // that everything below it has a value smaller than it, and everything
            // above it has a value greater than it.
            Unsorted[i + 1] = temp;

            // Now the array is sorted up to  the position j is pointing to and one
            // beyond, so we'll advance j by one position for the next round
        }
    }

    public static void main(String[] args) {
        sortMethod();
        for (int i = 0 ; i < Unsorted.length ; i++)
            System.out.println(Unsorted[i]);
    }
}

结果:

-999
-34
1
2
3
4
7
22
444

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2020-04-26
    • 2014-09-07
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多