import java.util.Scanner; import java.io.BufferedInputStream; class Main{ static int N = (int) 1e5 + 10,len; static int[] q = new int[N],temp = new int[N]; static void mergeSort(int[] q,int l,int r){ if(l>=r) return; int mid=(l+r)>>1; mergeSort(q,l,mid); mergeSort(q,mid+1,r); int k=0,i=l,j=mid+1; while(i<=mid&&j<=r) if(q[i]<q[j])temp[k++]=q[i++]; else temp[k++]=q[j++]; while(i<=mid) temp[k++]=q[i++]; while(j<=r)temp[k++]=q[j++]; for(k=0,i=l;i<=r;++i,++k) q[i]=temp[k]; } public static void main(String[] args){ Scanner sc=new Scanner(new BufferedInputStream(System.in)); len=sc.nextInt(); for(int x=0;x<len;++x) q[x]=sc.nextInt(); mergeSort(q,0,len-1); for(int x:q) System.out.print(x+" "); } }
来源:https://www.cnblogs.com/INnoVationv2/p/12432305.html