1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=1e6+10; 5 int n; 6 int q[N]; 7 void quick_sort(int q[],int l,int r){ 8 if(l>=r){ 9 return ; 10 } 11 int x=q[l+r>>1],i=l-1,j=r+1; 12 while(i<j){ 13 do i++; while(q[i]<x); 14 do j--; while(q[j]>x); 15 if(i<j){ 16 swap(q[i],q[j]); 17 } 18 } 19 quick_sort(q,l,j); 20 quick_sort(q,j+1,r); 21 } 22 int main(void){ 23 scanf("%d",&n); 24 for(int i=0;i<n;i++){ 25 scanf("%d",&q[i]); 26 } 27 quick_sort(q,0,n-1); 28 for(int i=0;i<n;i++){ 29 printf("%d ",q[i]); 30 } 31 return 0; 32 }
#include<iostream> #include<cstdio> using namespace std; const int N=1e6+10; int n; int q[N]; int tmp[N]; void merge_sort(int q[],int l,int r){ if(l>=r){ return ; } int mid = l+r>>1; merge_sort(q,l,mid); merge_sort(q,mid+1,r); int k=0; int i=l,j=mid+1; while(i<=mid&&j<=r){ if(q[i]<q[j]) tmp[k++]=q[i++]; else tmp[k++]=q[j++]; } while(i<=mid) tmp[k++]=q[i++]; while(j<=r) tmp[k++]=q[j++]; for(i=l,j=0;i<=r;i++,j++) q[i]=tmp[j]; } int main (void){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&q[i]); } merge_sort(q,0,n-1); for(int i=0;i<n;i++){ printf("%d ",q[i]); } return 0; }