1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function quickSort(arr){ let left = []; let right = []; var base = arr[arr.length - 1]; if (arr.length <= 1) {return arr}; for (let i = 0; i < arr.length-1; i++) { if (arr[i] < base) { left.push(arr[i]); }else{ right.push(arr[i]); } } return [...quickSort(left),...[base],...quickSort(right)]; } console.log (quickSort([1,33,55,0,12,33,77]));//[0, 1, 12, 33, 33, 55, 77]
|