51Nod-1018——暑假训练第一场(I)

16 篇文章 0 订阅

题目描述:

给出N个整数,对着N个整数进行排序

Input

第1行:整数的数量N(1 <= N <= 50000) 
第2 - N + 1行:待排序的整数(-10^9 <= Aii <= 10^9)

Output

共n行,按照递增序输出排序好的数据。

Sample Input

5
5
4
3
2
1

Sample Output

1
2
3
4
5

这道题的数据有些大,所以如果用冒泡排序的话,会TLE。因此需采取其他排序方法,而且数组的大小也很扯,试了好久才过。

import java.util.*;
public class Main
{
	public static void merge(int a[],int l,int m,int h)
	{
		int temp[] = new int[h - l + 1];
		int i = l;
		int j = m + 1;
		int k = 0;
		while(i <= m && j <= h)
		{
			if(a[i] < a[j])
			{
				temp[k++] = a[i++];
			}
			else
			{
				temp[k++] = a[j++];
			}
		}
		while(i <= m)
		{
			temp[k++] = a[i++];
		}
		while(j <= h)
		{
			temp[k++] = a[j++];
		}
		for(int x = 0;x < temp.length;x++)
		{
			a[x + l] = temp[x];
		}
	}
	public static int[] sort(int a[],int l,int h)
	{
		int m = (l + h) / 2;
		if(l < h)
		{
			sort(a,l,m);
			sort(a,m + 1,h);
			merge(a,l,m,h);
		}
		return a;
	}
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a[] = new int [50014];
		for(int i = 0;i < n;i++)
		{
			a[i] = sc.nextInt();
		}
		sort(a,0,n - 1);
		for(int i = 0;i < n;i++)
		{  
			System.out.println(a[i]);
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值