const mergeSort = arr => {
const rec = gap => {
if (gap.length === 1) {
return gap
}
const mid = gap.length >>> 1
const left = gap.slice(0, mid)
const right = gap.slice(mid, gap.length)
const orderLeft = rec(left)
const orderRight = rec(right)
const res = []
while (orderLeft.length || orderRight.length) {
if (orderLeft.length && orderRight.length) {
res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift())
} else if (orderLeft.length) {
res.push(orderLeft.shift())
} else if (orderRight.length) {
res.push(orderRight.shift())
}
}
return res
}
return rec(arr)
}
mergeSort([6,5,4,3,2,1])