归并排序模板
code:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL q[N],tmp[N];
int n;
void merge_sort(LL * 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 i=l,j=mid+1,k=0;
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,k=0;i<=r;i++,k++) q[i] = tmp[k];
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lld",&q[i]);
merge_sort(q,0,n-1);
for(int i=0;i<n;i++) printf("%lld ",q[i]);
return 0;
}