编辑代码

function mergeSort(arr) {
    if(arr.length === 1){
        return arr
    }
    let mid = Math.floor(arr.length/2)
    let left = arr.slice(0,mid)
    let right = arr.slice(mid)
    return merge(mergeSort(left),mergeSort(right))
}
function merge(a, b) {
    let res = [];

    while (a.length && b.length) {
        if (a[0] < b[0]) {
            res.push(a[0]);
            a.shift()
        } else {
            res.push(b[0]);
            b.shift()
        }
    }
    if(a.length){
        res = res.concat(a)
    }else{
        res = res.concat(b)
    }
    return res
}
let arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
console.log(mergeSort(arr))