题解 | #成绩排序# 手搓归并 + 使用库

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

看到需要稳定的排序 + logN,立刻想到归并,但是默认的 Arrays.sort() 使用的是快排,并不稳定,我于是手搓了一个:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    // 稳定并且时间复杂度为 logN 的算法是归并
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int order = in.nextInt();

            List<Student> list = new ArrayList<>(n);
            for (int i = 0; i < n; i++) {
                String name = in.next();
                int score = in.nextInt();
                Student student = new Student(name, score);
                list.add(student);
            }

            List<Student> ans = sort(list, order);

            ans.forEach(System.out::println);
        }
    }

    public static List<Student> sort(List<Student> list, int order) {

        int n = list.size();
        if (n <= 1) return list;

        List<Student> list1 = list.subList(0, n / 2);
        List<Student> list2 = list.subList(n / 2, n);
        list1 = sort(list1, order);
        list2 = sort(list2, order);

        List<Student> ans = new ArrayList<>(n);
        int i, j;
        i = j = 0;
        while (i < list1.size() || j < list2.size()) {
            if (j == list2.size() ||  ( i < list1.size() &&
                                        (order == 0 ? list1.get(i).score >= list2.get(j).score : list1.get(
                                             i).score <= list2.get(j).score)
                                      )) {
                ans.add(list1.get(i));
                i++;
            } else {
                ans.add(list2.get(j));
                j++;
            }
        }
        return ans;
    }


    public static class Student {
        String name;
        int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
            return name + " " + score;
        }
    }
}

后来翻看资料,发现 Collections.sort() 使用的就是归并排序 o.O

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int order = in.nextInt();

            List<Student> list = new ArrayList<>(n);
            for (int i = 0; i < n; i++) {
                String name = in.next();
                int score = in.nextInt();
                Student student = new Student(name, score);
                list.add(student);
            }

            if (order == 0) list.sort((a, b) -> b.score - a.score);
            else list.sort((a, b) -> a.score - b.score);

            list.forEach(System.out::println);
        }
    }

    public static class Student {
        String name;
        int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
            return name + " " + score;
        }
    }
}
全部评论

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
我见java多妩媚:大外包
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务