java - 在java快速排序中添加额外的排序条件

标签 java algorithm sorting quicksort

所以我有这段代码可以按年级对学生列表进行快速排序。

public static void quickSort(Student[] school){
    quickSort(school, 0, school.length - 1);              // quicksort all the elements in the array
}

private static void quickSort(Student[] school, int start, int end) {
    int i = start;                          // index of left-to-right scan
    int k = end;                            // index of right-to-left scan

    if (end - start >= 1) // check that there are at least two elements to sort
    {
        Student pivot = school[start];       // set the pivot as the first element in the partition

        while (k > i) // while the scan indices from left and right have not met,
        {
            while (school[i].getStudentGrade() <= pivot.getStudentGrade() && i <= end && k > i) // from the left, look for the first
            {
                i++;
                                                   // element greater than the pivot
            }
            while (school[k].getStudentGrade() > pivot.getStudentGrade() && k >= start && k >= i) // from the right, look for the first
            {
                k--;                                        // element not greater than the pivot
            }
            if (k > i) // if the left seekindex is still smaller than
            {
                swap(school, i, k);                      // the right index, swap the corresponding elements
            }
        }
        swap(school, start, k);          // after the indices have crossed, swap the last element in
        // the left partition with the pivot 
        quickSort(school, start, k - 1); // quicksort the left partition
        quickSort(school, k + 1, end);   // quicksort the right partition
    } else // if there is only one element in the partition, do not do any sorting
    {
        return;                     // the array is sorted, so exit
    }
}

//Swap 2 index values in array
private static void swap(Student[] school, int index1, int index2)
{
    Student temp = school[index1];           
    school[index1] = school[index2];    
    school[index2] = temp;               
}

我只是不知道如何添加额外的排序标准,以便根据我通过使用 student.getStudentNumber 获得的学号对具有相同年级的学生进行排序。

最佳答案

不是直接使用比较函数,而是传递一个 java.util.Comparator<T> . 这样你就可以实现不同的 Comparator s 用于不同的排序标准。

比较器看起来像这样:

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

    @Override
    public int compare(final Student s1, final Student s2) {
        final int gradeDiff = s1.getStudentGrade() - s2.getStudentGrade();
        if (0 != gradeDiff) {
            return gradeDiff;
        }
        final int numberDiff = s1.getStudentNumber() - s2.getStudentNumber();
        if (0 != numberDiff) {
            return numberDiff;
        }
        // addd mor criteria here if wanted
        return 0;
    }
}

关于java - 在java快速排序中添加额外的排序条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650878/

相关文章:

java - eclipse Spring MVC 4 +Hibernate 和 MySQL + Tiles.xml 中的 HTTP 状态 404

algorithm - 二分查找与简单查找

java - 如何在 Spring 中为 View 路径设置自定义 View 解析器

java - 问题 Android Studio 1.0.2 : Fatal Exception: main Process java. lang.NullPointerException

java - 计算不同切片的数量(仅包含唯一数字)

algorithm - 计算网格中标记节点 k 距离内的节点

python - 如何找到在两种不同语言中具有相同外观的所有单词?

java - 当我的 Integer[] 已经初始化时,为什么会出现 NullPointerException?

sorting - 功能样式中最快的排序

java - 在Android中使用千位分隔符(,)输入时如何格式化EditText的输入?