let arr = [0,9,7,5]; function sort(arr){ if(arr.length < 2) return arr; let midIndex = Math.floor(arr.length/2); let midValue = arr.splice(midIndex,1)[0]; let left = []; let right = []; for(let i=0;i<arr.length;i++){ if(arr[i]<midValue){ left.push(arr[i]) }else{ right.push(arr[i]); } } return [...sort(left),midValue,...sort(right)]; } console.log(sort(arr))