简体   繁体   English

Java 快速排序 - 是否可以在不使用比较器的情况下比较对象?

[英]Java quicksort - is it possible to compare objects without using a comparator?

Is there a way to quickSort the elements of an array in an ArrayList and at the same time change the order of the ArrayList based on the array sorted without using the Comparator<> function?有没有办法快速排序ArrayList中的数组元素,同时根据不使用Comparator<> ZC1C425268E68385D1AB5074C17A94F 排序的数组更改ArrayList的顺序?

    public ArrayList<PatientArray> ageSorter(ArrayList<PatientArray> pa) {
        if (pa.size() <= 1) {
            return pa;
        }

        ArrayList<PatientArray> sorted;
        ArrayList<PatientArray> smaller = new ArrayList<PatientArray>();
        ArrayList<PatientArray> greater = new ArrayList<PatientArray>();

        PatientArray middle = pa.get(0);
        int i;
        PatientArray j;
        for (i = 1; i < pa.size(); i++) {
            j = pa.get(i);

            if ((new SortAge().compare(j, middle)) < 0) { // this object comparator
                smaller.add(j);
            } else {
                greater.add(j);
            }
        }
        smaller = ageSorter(smaller);
        greater = ageSorter(greater);
        smaller.add(middle);
        smaller.addAll(greater);
        sorted = smaller;

        return sorted;
    }

    class SortAge implements Comparator <PatientArray>{
    public int compare(PatientArray a1, PatientArray a2){
        return a1.age-a2.age;
    }

The simplest way to avoid use of a Comparator would be to perform the comparison yourself directly in your quicksort code:避免使用Comparator的最简单方法是直接在您的快速排序代码中执行比较:

if (pa.get(i).age < middle.age)

While you haven't asked for general review comments, I'll note that there are lots of unnecessary commands in your code.虽然您没有要求提供一般审查意见,但我会注意到您的代码中有很多不必要的命令。

public ArrayList<PatientArray> ageSorter(ArrayList<PatientArray> pa) {
    if (pa.size() <= 1) {
        return pa;
    }

    ArrayList<PatientArray> smaller = new ArrayList<PatientArray>();
    ArrayList<PatientArray> greater = new ArrayList<PatientArray>();

    PatientArray pivot = pa.get(0);
    for (int i = 1; i < pa.size(); i++) {
        if (pa.get(i).age < pivot.age) {
            smaller.add(j);
        } else {
            greater.add(j);
        }
    }
    smaller = ageSorter(smaller);
    greater = ageSorter(greater);
    smaller.add(middle);
    smaller.addAll(greater);
    return smaller;
}

Also note that typically quicksort is implemented so that the sorting is done in-place - ie without creating new arrays.另请注意,通常会实施快速排序,以便就地完成排序 - 即无需创建新的 arrays。

As @Holger points out in comments below, choice of pivot (as first element) is also poor.正如@Holger 在下面的评论中指出的那样,pivot(作为第一个元素)的选择也很差。 The reasons, and alternatives, are explained here这里解释了原因和替代方案

While technically your algorithm is quicksort it's probably not quick.虽然从技术上讲,您的算法是快速排序的,但它可能并不快。

You can use the sort method on List class introduced on Java 8 .您可以使用Java 8上介绍的列表class 上的sort方法。 So your method will be as follow:所以你的方法如下:

public List<PatientArray> ageSorter(ArrayList<PatientArray> pa) {
   pa.sort(Comparator.comparingInt(a -> a.age));
   return pa;
}

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

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