快排思路:找一个基准对比元素,循环得到比它小的和比它大的两个list,递归地把小于基准值元素的子数列和大于基准值元素的子数列排序
const quickSort = (array) => {
// 边界处理
if (array.length <= 1) return array
const midIndex = Math.floor(array.length / 2)
const midEle = array[midIndex]
const left = []
const right = []
for (let i = 0; i< array.length; i++) {
if (i === midIndex) continue
if (array[i] < midEle) {
left.push(array[i])
} else {
right.push(array[i])
}
}
return [...quickSort(left), midEle, ...quickSort(right)]
}