归并排序
很久没打了,现在发现思路跟线段树差不多
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int) 1e6+7;
int N;
int A [MAXN];
int Tmp[MAXN];
void Merge(int l,int r){
if (l == r) {
Tmp[l] = A[l];
return ;
}
int m = (l+r)>>1;
Merge(l,m);
Merge(m+1,r);
int i = l,j = m+1;
int t = l;
while (i <= m && j <= r){
if (Tmp[i] < Tmp[j]){
A[t++] = Tmp[i++];
}else {
A[t++] = Tmp[j++];
}
}
while (i <= m) A[t++] = Tmp[i++];
while (j <= r) A[t++] = Tmp[j++];
for (int i = l;i <= r;i ++)Tmp[i] = A[i];
}
int main()
{
scanf("%d",&N);
for (int i = 1;i <= N;i ++) scanf("%d",&A[i]);
Merge(1,N);
for (int i = 1;i <= N;i ++) printf("%d%c",A[i],i==N?'\n':' ');
}