📅  最后修改于: 2022-03-11 15:02:26.940000             🧑  作者: Mango
function quickSortMain2(array) {
console.log("array", array);
var l = array.length;
if(l <= 1) return array;
const pivot = array[l-1];
const leftArr = [];
const rightArr = [];
for (const el of array.slice(0, l-1)) el < pivot ? leftArr.push(el) : rightArr.push(el);
if(leftArr.length > 0 && rightArr.length > 0) return [...quickSortMain2(leftArr), pivot, ...quickSortMain2(rightArr)];
else if(leftArr.length) return [...quickSortMain2(leftArr), pivot];
else if(rightArr.length > 0) return [pivot, ...quickSortMain2(rightArr)];
}
var items = [5,3,7,6,2,9];
var sortedArray = quickSortMain2(items);
console.log(sortedArray);