1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function quickSort(arr){ if(arr.length<=1){ return arr; } var baseIndex = Math.floor(arr.length/2); var base = arr.splice(baseIndex,1) var leftArr = []; var rightArr = []; for(var i=0;i<arr.length;i++){ if(arr[i]<base){ leftArr.push(arr[i]) }else{ rightArr.push(arr[i]) } } return quickSort(leftArr).concat(base,quickSort(rightArr)) }
|