1 function quickSort(arr){ 2 //取基準 3 var pointIndex=Math.floor(arr.length/2); 4 //數組去除基準元素,並取出其值 5 var pointValue=arr.splice(pointIndex,1)[0]; 6 var left=[], 7 right=[]; 8 for(var i=0,length=arr.length;i<length;i++){ 9 arr[i]<pointValue?left.push(arr[i]):right.push(arr[i]); 10 } 11 return quickSort(left).concat([pointValue],quickSort(right)); 12 }
基準
遍歷分出兩個數組
遞歸數組