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=[]; //小的放left,大的放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)); //大到小 //return quickSort(right).concat([pivot],quickSort(left)); }