function sort(nums){ if(nums.length < 2) return nums; let mid = Math.floor(nums.length / 2); let left = [], right = []; let temp = nums.splice(mid, 1)[0]; for(let i = 0; i < nums.length; i++){ if(nums[i] < temp){ left.push(nums[i]); }else{ right.push(nums[i]) } } return sort(left).concat([temp], sort(right)); } nums = [5,2,6,7,3,5,1]; console.log(sort(nums))