简体   繁体   English

使用插入排序对随机数数组进行排序

[英]Sort Array of random numbers using insertion sort

Trouble Shooting My insertionSort故障排除我的插入排序

Here is my code...这是我的代码...

        ArrayList<Integer> num2 = new ArrayList<Integer>();
        for (int b = 0; b < 300; b++)
        {
            num2.add((int)(Math.random() * 1000));
        }
        if (choice == 2)
        {
            System.out.println("Insertion Sort chosen\nUnsorted Array List: " + num2);
            insertionSort(num2);
            System.out.println("Insertion sorted Array List: " + num2);
        }//choice 2 end
public static void insertionSort(ArrayList<Integer> num2)
{
    for(int f = 1; f < num2.size(); f++)
    {
        int key = num2.get(f);
        for (int g = f-1; g >= 0; g--)
        {
            if (key < num2.get(g))
            {
                num2.set(g+1, num2.get(g));
                if (g == 0)
                {
                    num2.set(0, key);
                }
            }
            else
            {
                num2.set(g+1, key);
            }
        }
    }
}

Output: Output:
Insertion Sort chosen已选择插入排序
Unsorted Array List: [42, 944, 500, 442, 277, 178, 881, 267, 705, 365, 481, 882, 221, 835, 329, 312, 97, 1,...未排序的数组列表:[42, 944, 500, 442, 277, 178, 881, 267, 705, 365, 481, 882, 221, 835, 329, 312, 97, 1,...
Insertion sorted Array List: [1, 479, 479, 479, 479, 718, 785, 785, 988, 988, 988, 988, 988, 988, 988, 988, 988,...插入排序数组列表:[1, 479, 479, 479, 479, 718, 785, 785, 988, 988, 988, 988, 988, 988, 988, 988, 988,...


if anyone can help me figure out why a bunch of the larger numbers just start repeating themselves because it is frustrating.如果有人能帮我弄清楚为什么一堆更大的数字只是开始重复自己,因为这令人沮丧。

When you cannot find where the problems is, you can make use of the breakpoint to help you.当您找不到问题所在时,您可以利用断点来帮助您。 Set one or several breakpoints where you think the codes here are critical.在您认为此处的代码至关重要的地方设置一个或多个断点。 For example, when you debug your insertionSort function, you can set your breakpoint at Line 5 to see what changes the inner loop have made on your array.例如,当您调试您的insertionSort排序 function 时,您可以在第 5 行设置断点以查看内部循环对您的数组所做的更改。 And you will find that the inner loop have done something outside your expectation.你会发现内循环做了一些超出你预期的事情。 Then you can continue to set the breakpoint inside the inner loop to see where the problem is.然后可以继续在内循环里面设置断点,看看问题出在哪里。

Actually, your problem is that when you find the right position to put your key , you should break the inner loop.实际上,您的问题是,当您找到正确的 position 来放置您的key时,您应该打破内循环。 Otherwise, since the key is larger than all the numbers before the proper position, all the numbers before except the first one (since your code is num2.set(g+1, key); , and when g = 0, it changes the second element) will be replaced by the key .否则,由于key大于正确的 position 之前的所有数字,因此除了第一个数字之前的所有数字(因为您的代码是num2.set(g+1, key); ,并且当 g = 0 时,它会更改第二个元素)将被替换为key

So, you only need to add a break .所以,你只需要添加一个break

public static void insertionSort(ArrayList<Integer> num2)
{
    for(int f = 1; f < num2.size(); f++)
    {
        int key = num2.get(f);
        for (int g = f-1; g >= 0; g--)
        {
            if (key < num2.get(g))
            {
                num2.set(g+1, num2.get(g));
                if (g == 0)
                {
                    num2.set(0, key);
                }
            }
            else
            {
                num2.set(g+1, key);
                break;
            }
        }
    }
}

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

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