AcWing 787. 归并排序模板
原题链接
简单
作者:
IAMI
,
2020-04-04 09:40:59
,
所有人可见
,
阅读 2
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int P[N], t[N];
void merge_sort(int p[], int l, int r)
{
if(l >= r) return;
int mid = l+r>>1;
merge_sort(p, l, mid);
merge_sort(p, mid+1, r);
int k=0, i = l, j = mid + 1;
while(i<=mid && j<=r)
{
if(p[i]<p[j])
t[k++] = p[i++];
else
t[k++] = p[j++];
}
while(i<=mid) t[k++] = p[i++];
while(j<=r) t[k++] = p[j++];
for(i=l, j=0; i<=r; i++, j++) p[i] = t[j];
}
int main()
{
int n;
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d", &P[i]);
merge_sort(P, 0, n-1);
for(int i=0; i<n; i++) printf("%d ", P[i]);
return 0;
}