You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现含重复部门的Staff数组快速排序:部门重复时按ID升序

问题

我用快速排序对Staff对象数组排序,当前只按部门(dep)字段排序,但数组里有重复的部门值。我想实现:部门值重复时,按ID字段升序排序,请问怎么修改代码?现有代码如下:

public class Main {
    public static void main(String[] args) {
        Staff[] staffs = new Staff[10];
        Staff[] answer = new Staff[staffs.length];
        int temp = 0;
        
        
        staffs[0] = new Staff(1,"J","IT");
        staffs[1] = new Staff(3,"I","IT");
        staffs[2] = new Staff(2,"H","GA");
        staffs[3] = new Staff(7,"G","GA");
        staffs[4] = new Staff(5,"F","AUDIT");
        staffs[5] = new Staff(6,"E","AUDIT");
        staffs[6] = new Staff(4,"D","IT");
        staffs[7] = new Staff(10,"C","IT");
        staffs[8] = new Staff(9,"B","HR");
        staffs[9] = new Staff(8,"A","HR");
        
        
        PrintArray(staffs);
        quickSort(staffs, 0, staffs.length-1);
        PrintArray(staffs);
    }
    private static void quickSort(Staff[] staffs, int start, int end)
    {

            int i = start;
            int j = end;
            if (j - i >= 1)
            {
                String pivot = staffs[i].dep;
                while (j > i)
                {

                    while (staffs[i].dep.compareTo(pivot) <= 0 && i < end && j > i){
                        i++;
                    }

                    while (staffs[j].dep.compareTo(pivot) >= 0 && j > start && j >= i){
                        j--;
                    }
                    
                    if (j > i)
                        swap(staffs, i, j);
                }

            
                swap(staffs, start, j);
                quickSort(staffs, start, j - 1);
                quickSort(staffs, j + 1, end);
            }
        }

        private static void swap(Staff[] staffs, int i, int j)
        {
        Staff temp = staffs[i];
        staffs[i] = staffs[j];
        staffs[j] = temp;
        }
    public static void PrintArray(Staff[] staffs) {
        for(int a = 0; a<staffs.length; a++) {
            System.out.println(staffs[a].id+", "+staffs[a].name+", "+staffs[a].dep);
        }
        System.out.println("");
    }

}
修改方案

核心是调整快速排序中的比较逻辑:原来只判断部门字段,现在需要先比部门,部门相同时再比较ID的大小。具体修改点:

  • 把基准值从单独的部门字符串改成整个Staff对象,方便同时获取部门和ID信息
  • 重写两个while循环的判断条件,增加ID的比较逻辑
修改后的完整代码
public class Main {
    public static void main(String[] args) {
        Staff[] staffs = new Staff[10];
        Staff[] answer = new Staff[staffs.length];
        int temp = 0;
        
        
        staffs[0] = new Staff(1,"J","IT");
        staffs[1] = new Staff(3,"I","IT");
        staffs[2] = new Staff(2,"H","GA");
        staffs[3] = new Staff(7,"G","GA");
        staffs[4] = new Staff(5,"F","AUDIT");
        staffs[5] = new Staff(6,"E","AUDIT");
        staffs[6] = new Staff(4,"D","IT");
        staffs[7] = new Staff(10,"C","IT");
        staffs[8] = new Staff(9,"B","HR");
        staffs[9] = new Staff(8,"A","HR");
        
        
        PrintArray(staffs);
        quickSort(staffs, 0, staffs.length-1);
        PrintArray(staffs);
    }
    
    private static void quickSort(Staff[] staffs, int start, int end) {
        int i = start;
        int j = end;
        if (j - i >= 1) {
            // 改用整个Staff对象作为基准,同时获取dep和id信息
            Staff pivot = staffs[i];
            while (j > i) {
                // 先比部门,部门相等则比ID,满足<=基准才移动指针i
                while ((staffs[i].dep.compareTo(pivot.dep) < 0 
                        || (staffs[i].dep.equals(pivot.dep) && staffs[i].id <= pivot.id)) 
                        && i < end && j > i) {
                    i++;
                }
                // 先比部门,部门相等则比ID,满足>=基准才移动指针j
                while ((staffs[j].dep.compareTo(pivot.dep) > 0 
                        || (staffs[j].dep.equals(pivot.dep) && staffs[j].id >= pivot.id)) 
                        && j > start && j >= i) {
                    j--;
                }
                
                if (j > i)
                    swap(staffs, i, j);
            }
            swap(staffs, start, j);
            quickSort(staffs, start, j - 1);
            quickSort(staffs, j + 1, end);
        }
    }

    private static void swap(Staff[] staffs, int i, int j) {
        Staff temp = staffs[i];
        staffs[i] = staffs[j];
        staffs[j] = temp;
    }
    
    public static void PrintArray(Staff[] staffs) {
        for(int a = 0; a<staffs.length; a++) {
            System.out.println(staffs[a].id+", "+staffs[a].name+", "+staffs[a].dep);
        }
        System.out.println("");
    }
}

// 补充原代码省略的Staff类定义
class Staff {
    int id;
    String name;
    String dep;
    
    public Staff(int id, String name, String dep) {
        this.id = id;
        this.name = name;
        this.dep = dep;
    }
}
效果说明

修改后排序逻辑会先按部门字典序升序排列,当部门相同时自动按ID从小到大排序。比如IT部门的员工会按ID 1、3、4、10的顺序排列,其他部门也遵循相同规则。

内容的提问来源于stack exchange,提问作者user21490683

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:17:55