787. 归并排序
题目链接https://www.acwing.com/problem/content/789/
题目:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
void merge_sort(int a[],int l,int r){
if(l==r) return;
int mid=l+r>>1;
merge_sort(a,l,mid);
merge_sort(a,mid+1,r);
int t[r-l+1],k=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r){
if(a[i]<a[j]) t[k++]=a[i++];
else t[k++]=a[j++];
}
while(i<=mid) t[k++]=a[i++];
while(j<=r) t[k++]=a[j++];
for(int i=0;i<k;i++)
a[l+i]=t[i];
}
int main(){
int n;
int a[100010];
cin>>n;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
for(int i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}