AcWing 787. 【归并排序模板】
原题链接
简单
#include<iostream>
using namespace std;
const int N = 1e6+10;
int n, s[N], help[N];
void merge(int L, int mid, int R){
int p1 = L, p2 = mid + 1, i = 0;
while(p1 <= mid && p2 <= R)
help[i++] = s[p1] <= s[p2] ? s[p1++] : s[p2++];
while(p1 <= mid) help[i++] = s[p1++];
while(p2 <= R) help[i++] = s[p2++];
for(int j = 0; j < i; j++) s[L + j] = help[j];
}
void sortProcess(int L, int R){
if(L == R) return;
int mid = L + ((R - L) >> 1);
sortProcess(L, mid);
sortProcess(mid + 1, R);
merge(L, mid, R);
}
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> s[i];
sortProcess(0, n-1);
for(int i = 0; i < n; i++) cout << s[i] << " ";
}