function sort(target) { if (!Array.isArray(nums)) throw 'not array' else if (target.length <= 1) return target const middleIndex = Math.floor(target.length / 2); const middleNum = target.splice(middleIndex, 1)[0]; let left = [], right = [] target.forEach((item) => { if (item < middleNum) left.push(item) else right.push(item) }) return [...sort(left), middleNum, ...sort(right)] } console.log(sort([5, 1, 3, 4, 2]))