归并排序
作者:
anbceb
,
2024-05-17 19:46:13
,
所有人可见
,
阅读 11
#include <iostream>
#include <cstdio>
using namespace std;
const int N=1e6+10;
int q[N],tmp[N],n;
void ms(int q[],int l,int r)
{
if(l>=r)return;
int mid=l+r>>1;
ms(q,l,mid);
ms(q,mid+1,r);
int k=0,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(int w=l,o=0;w<=r;w++,o++)q[w]=tmp[o];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&q[i]);
ms(q,1,n);
for(int i=1;i<=n;i++)printf("%d ",q[i]);
return 0;
}