题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#

输入整型数组和排序标识,对其元素按照升序或降序进行排序

http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309

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

相关推荐

如题如果提出了一个薪资,A不成功,会有可能被取消offer吗
爱打瞌睡的柯基:最想去你们公司 但是别家开的高一些,希望能申请高一点 不管结果如何都谢谢你
点赞 评论 收藏
分享
手撕没做出来是不是一定挂
Chrispp3:不会,写出来也不一定过
点赞 评论 收藏
分享
10-14 10:56
已编辑
长沙学院 嵌入式软件开发
痴心的00后拿到了ssp:hr面挂了,无所谓了反正不去😃
点赞 评论 收藏
分享
爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务