let tmp = [];
function mergesort(nums, L, R) {
if(L < R) {
let mid = (L+R) >> 1;
mergesort(nums, L, mid);
mergesort(nums, mid+1, R);
let i = L;
let j = mid+1;
let k = 0;
while(i<=mid && j<=R) {
if(nums[i] < nums[j]) tmp[k++] = nums[i++];
else tmp[k++] = nums[j++];
}
while(i<=mid) tmp[k++] = nums[i++];
while(j<=R) tmp[k++] = nums[j++];
let m = L;
let n = 0;
for(;m<=R;m++,n++) nums[m] = tmp[n];
}
}
const readline = require("readline");
const R = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const LOG = console.log;
let cnt = 0;
let n = 0;
R.on("line", function(line) {
line = line.trim();
cnt++;
if(cnt === 1) n = parseInt(line);
if(cnt === 2) {
let nums = line.split(" ");
for(let i=0;i<nums.length;i++) nums[i] -= "0";
mergesort(nums, 0, n-1);
let res = nums.join(" ");
LOG(res);
}
});
AcWing 787. 归并排序
最新推荐文章于 2022-10-25 16:56:37 发布