package fenshujs;
import java.util.Arrays;
import java.util.Scanner;
/*
* 归并排序适用于大数据的排序,可以先把数据进行分割,在各个线程中分别排序,最后再合并
* 当然,这个运算放在CPU中不太合适,因为CPU擅长串行计算,而GPU擅长并行计算,可以用GPU
* 然而现实中常常是多个机器并行计算
*/
public class bishi {
private static void MergeSort(int[] a,int left,int right)
{
if(left >= right)
{
return;
}
MergeSort(a,left,(left+right)/2);
MergeSort(a,(left+right)/2+1,right);
Merge(a,left,(left+right)/2,(left+right)/2+1,right);
}
private static void Merge(int[] a,int left1,int right1,int left2,int right2)
{
int i,j,index;
for(i = left1,j = left2,index = i;i<=right1&&j<=right2;)
{
if(a[i]<a[j])
{
aa[index] = a[i];
i++;
}
else
{
aa[index] = a[j];
j++;
}
index++;
}
if(i > right1)
{
while(index<=right2)
{
aa[index] = a[j];
j++;
index++;
}
}
else
{
while(index<=right2)
{
aa[index] = a[i];
i++;
index++;
}
}
for(int k = left1;k<=right2;k++)
{
a[k] = aa[k];
}
}
private static int[] aa;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
aa = new int[n];
for(int i = 0;i<n;i++)
a[i] = sc.nextInt();
MergeSort(a,0,a.length-1);
for(int a1:aa)
System.out.print(a1+" ");
}
import java.util.Arrays;
import java.util.Scanner;
/*
* 归并排序适用于大数据的排序,可以先把数据进行分割,在各个线程中分别排序,最后再合并
* 当然,这个运算放在CPU中不太合适,因为CPU擅长串行计算,而GPU擅长并行计算,可以用GPU
* 然而现实中常常是多个机器并行计算
*/
public class bishi {
private static void MergeSort(int[] a,int left,int right)
{
if(left >= right)
{
return;
}
MergeSort(a,left,(left+right)/2);
MergeSort(a,(left+right)/2+1,right);
Merge(a,left,(left+right)/2,(left+right)/2+1,right);
}
private static void Merge(int[] a,int left1,int right1,int left2,int right2)
{
int i,j,index;
for(i = left1,j = left2,index = i;i<=right1&&j<=right2;)
{
if(a[i]<a[j])
{
aa[index] = a[i];
i++;
}
else
{
aa[index] = a[j];
j++;
}
index++;
}
if(i > right1)
{
while(index<=right2)
{
aa[index] = a[j];
j++;
index++;
}
}
else
{
while(index<=right2)
{
aa[index] = a[i];
i++;
index++;
}
}
for(int k = left1;k<=right2;k++)
{
a[k] = aa[k];
}
}
private static int[] aa;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
aa = new int[n];
for(int i = 0;i<n;i++)
a[i] = sc.nextInt();
MergeSort(a,0,a.length-1);
for(int a1:aa)
System.out.print(a1+" ");
}
}
10
1 10 2 9 3 8 4 7 5 6
1 2 3 4 5 6 7 8 9 10