题解 | #成绩排序#

成绩排序

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

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = Integer.valueOf(scan.nextLine().trim());
        int sign = Integer.valueOf(scan.nextLine().trim());
        String[][] nameList = new String[n][2];
        for (int i = 0; i < n; i++) {
            String[] ns =  scan.nextLine().split(" ");
            nameList[i][0] = ns[0];
            nameList[i][1] = ns[1];
        }
        mergeSort(nameList, sign);
        for (String[] ns : nameList) {
            System.out.println(ns[0] + " " + ns[1]);
        }
    }
    public static void mergeSort(String[][] strs, int sign) {
        if (null == strs || strs.length < 2) {
            return;
        }
        process(strs, 0, strs.length - 1, sign);
    }
    public static void process(String[][] strs, int start, int end, int sign) {
        if (start >= end) {
            return;
        }
        int mid = start + ((end - start) >> 1);
        process(strs, start, mid, sign);
        process(strs, mid + 1, end, sign);
        if (sign == 1) {
            merge1(strs, start, mid, end);
        } else {
            merge2(strs, start, mid, end);
        }
    }
    public static void merge1(String[][] strs, int start, int mid, int end) {
        String[][] helper = new String[end - start + 1][2];
        int index = 0;
        int p1 = start;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {
            if (Integer.valueOf(strs[p1][1]) <= Integer.valueOf(strs[p2][1])) {
                helper[index][0] = strs[p1][0];
                helper[index][1] = strs[p1][1];
                index++;
                p1++;
            } else {
                helper[index][0] = strs[p2][0];
                helper[index][1] = strs[p2][1];
                index++;
                p2++;
            }
        }
        while (p1 <= mid) {
            helper[index][0] = strs[p1][0];
            helper[index][1] = strs[p1][1];
            index++;
            p1++;
        }
        while (p2 <= end) {
            helper[index][0] = strs[p2][0];
            helper[index][1] = strs[p2][1];
            index++;
            p2++;
        }
        for (int i = 0; i < helper.length; i++) {
            strs[start + i][0] = helper[i][0];
            strs[start + i][1] = helper[i][1];
        }
    }
    public static void merge2(String[][] strs, int start, int mid, int end) {
        String[][] helper = new String[end - start + 1][2];
        int index = 0;
        int p1 = start;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {
            if (Integer.valueOf(strs[p1][1]) >= Integer.valueOf(strs[p2][1])) {
                helper[index][0] = strs[p1][0];
                helper[index][1] = strs[p1][1];
                index++;
                p1++;
            } else {
                helper[index][0] = strs[p2][0];
                helper[index][1] = strs[p2][1];
                index++;
                p2++;
            }
        }
        while (p1 <= mid) {
            helper[index][0] = strs[p1][0];
            helper[index][1] = strs[p1][1];
            index++;
            p1++;
        }
        while (p2 <= end) {
            helper[index][0] = strs[p2][0];
            helper[index][1] = strs[p2][1];
            index++;
            p2++;
        }
        for (int i = 0; i < helper.length; i++) {
            strs[start + i][0] = helper[i][0];
            strs[start + i][1] = helper[i][1];
        }
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-27 11:43

相关推荐

我在朝九晚六双休的联想等你:如果我是你,身体素质好我会去参军,然后走士兵计划考研211只需要200多分。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务