快速排序
- 时间复杂度 O(nlong(n))
function quickSort(arr:number[]):number[]{
const length = arr.length
if(legnth === 0) return arr
const midIndex = Math.floor(length / 2)
const midValue = arr.slice(midIndex,midIndex + 1)[0]
const left: number[] = []
const right: number[] = []
for(let i = 0; i < length; i++){
if(i !== midIndex){
const n = arr[i]
if(n < midValue){
//小于,则放在left
left.push(n)
}else{
//大于,则放在right
right.push(n)
}
}
}
return quickSort(left).concat(
[midValue],
quickSort(right)
)
}