Java 如何在不使用 collections.sort() 的情况下对数组列表进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23529720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:37:42  来源:igfitidea点击:

How can I sort an arraylist without using collections.sort()?

javasortingcollectionsarraylist

提问by Andy Reutzel

I have been looking for a while for a way to sort an arraylist without using collections.sort as my own logic is flawed and I have been having a lot of trouble.

我一直在寻找一种在不使用 collections.sort 的情况下对数组列表进行排序的方法,因为我自己的逻辑存在缺陷,并且遇到了很多麻烦。

I need to sort it in a way that I can use a method I created that basically does what collections.swap does in order to completely sort an arraylist.

我需要以一种可以使用我创建的方法对其进行排序,该方法基本上可以完成 collections.swap 所做的工作,以便对数组列表进行完全排序。

Here is my code:

这是我的代码:

public static void mySort(ArrayList<Double> sort){

    int min = 0;
    int i;
    int j = 0;
        for(i = 0; i < sort.size() - 1; i++) {
            min = i;
            mySwap(sort, j ,min);

            for(j = 0; j < sort.size() -1;j++){
                if(j < min ){
                    min = j;
                }
            }
    }
}

public static void mySwap(ArrayList<Double> a, int x, int y){

    double temp = a.get(x);
    a.set(x,a.get(y));
    a.set(y,temp);
}

I have been having a lot of trouble of with this. Sorry if it is a question is that harming the community.

我在这方面遇到了很多麻烦。对不起,如果这是一个问题,那就是在伤害社区。

采纳答案by Alexey Malev

I assume you want the following algorithm: find min in the rest of the array, swap it with current element starting with first, reconsider restto be array starting of increased +1 index.

我假设您需要以下算法:在数组的其余部分中找到 min,将其与从 first 开始的当前元素交换,重新考虑rest是增加 +1 索引的数组开始。

You should update your code like this:

你应该像这样更新你的代码:

public static void swap(List<Integer> sort, int i, int j) {
    int tmp = sort.get(i);
    sort.set(i, sort.get(j));
    sort.set(j, tmp);
}

public static void doSort(List<Integer> sort) {
    int min;
    for (int i = 0; i < sort.size(); ++i) {
        //find minimum in the rest of array
        min = i;
        for (int j = i + 1; j < sort.size(); ++j) {
            if (sort.get(j) < sort.get(min)) {
                min = j;
            }
        }

        //do swap
        swap(sort, i, min);
    }
}

You have a bug with finding minimum and then swapping items. Please note that the code can be improved in many ways (I tried to maintain your let's say way of coding as possible), such as swapping integer references in swap(), doing BubbleSortlike another answer suggests (same algorithm but simpler implementation), using O(n * log(n))complexity algorithm, and so on.

您在查找最小值然后交换项目时遇到了错误。请注意,代码可以通过多种方式改进(我试图保持你的让我们说尽可能的编码方式),例如在 中交换整数引用swap()BubbleSort像另一个答案建议的那样(相同的算法但更简单的实现),使用O(n * log(n))复杂性算法,等等。