1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| function quickSort(arr) { if (arr.length <= 1) return arr;
let pivot = arr.shift(); let leftArr = []; let rightArr = [];
for(let i=0; i<arr.length; i++) { if (arr[i] <= pivot) { leftArr.push(arr[i]); } else { rightArr.push(arr[i]); } }
return quickSort(leftArr).concat([pivot], quickSort(rightArr)); }
quickSort([13, 24, 0, 99, -1, 55, 4, 9, 2, 0]);
|