上面错下面对,为什么不能用上面的写法?
function quickSort(arr){ if(arr.length<=1){ return arr } var pivotIndex = Math.floor(arr.length/2) var left=[]; var right = []; arr.splice(pivotIndex,1) for(var i=0;i<arr.length;i++){ if(arr[i]<=arr[pivotIndex]){ left.push(arr[i]) }else{ right.push(arr[i]) } } return quickSort(left).concat([arr[pivotIndex]],quickSort(right)) } //-----------------------分割线-------------------------------- function quickSort(arr) { if(arr.length <= 1) { return arr; } var pivotIndex = Math.floor(arr.length / 2); var pivot = arr.splice(pivotIndex, 1)[0]; var left = []; var right = []; for(var i = 0; i < arr.length; i++) { if(arr[i] <= pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } return quickSort(left).concat([pivot], quickSort(right)); }
林逸舟丶
相关分类