1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function quickSort(arr) { const { length } = arr; if (length < 2) { return arr; }
let base = arr[0]; let min = arr.slice(1).filter((item) => item <= base); let max = arr.slice(1).filter((item) => item > base);
return quickSort(min).concat([base]).concat(quickSort(max)); }
const arr = [9, 3, 7, 4, 6];
console.log(quickSort(arr));
|