题目描述:
给出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 1Sample 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]);
}
}
}