SOURCE

console 命令行工具 X clear

                    
>
console
const merge = (left, right) => {
    const result = []
    while(left.length && right.length) {
        result.push(left[0] <= right[0] ? left.shift() : right.shift())
    }
    left.length && result.push(...left)
    right.length && result.push(...right)
    return result
}

const mergeSort = array => {
    const {length} = array
    if (length < 2) return array
    const midIndex = Math.floor(length / 2)
    const left = array.slice(0, midIndex)
    const right = array.slice(midIndex)
    return merge(mergeSort(left), mergeSort(right))
}

console.log(mergeSort([3,44,38,5,47,15,36,26,27,2,46,4,19,50,48]));