排序算法之快速排序(quick sort)

  • A+
所属分类:typescript 算法
function sort(arr: Array<number>): Array<number> {
    if (arr.length < 2) return arr
    let pivot = arr[~~(arr.length / 2)]
    let lower = []
    let median = []
    let upper = []
    arr.forEach(ele => {
        if (ele === pivot) median.push(ele)
        else if (ele > pivot) upper.push(ele)
        else lower.push(ele)
    })
    return [...sort(lower), ...median, ...sort(upper)]
}

export default sort
weinxin
我的微信
欢迎来撩!!
admin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: