【前端算法】用JS实现快速排序

简介: 理解数组方法里面运用到的算法,splice 和 slice的区别

固定算法,固定思路

  • 找到中间位置midValue
  • 遍历数组,小于midValue放在left,否则放在right
  • 继续递归。最后concat拼接,返回

细节

  • 获取midValue的两种方式:
  • 使用splice,会修改原数组
  • 使用slice,不会修改原数组——更加推荐

代码实现

  • splice方式
function quickSort1 (arr:number[]):number[] {
  const len = arr.length
  if (len === 0) return arr
   
  const midIdx = Math.floor(len / 2)
  const midValue = arr.splice(midIdx,1)[0]

  const left:number[] = []
  const right:number[] = []

  // 由于splice改变了原数组,所以不能使用len作为循环条件
  for (let i = 0; i < arr.length; i++) {
    
    if (arr[i] < midValue) {
      // 小于中间值,放left
      left.push(arr[i])
    } else {
      // 大于或者等于,放right
      right.push(arr[i])
    }
    
  }

  return quickSort1(left).concat([midValue],quickSort1(right))
}
  • slice方式
function quickSort2 (arr:number[]):number[] {
  const len = arr.length
  if (len === 0) return arr
   
  const midIdx = Math.floor(len / 2)
  const midValue = arr.slice(midIdx,midIdx + 1)[0]

  const left:number[] = []
  const right:number[] = []

  for (let i = 0; i < len; i++) {
    if (i !== midIdx) {
      if (arr[i] < midValue) {
        // 小于中间值,放left
        left.push(arr[i])
      } else {
        // 大于或者等于,放right
        right.push(arr[i])
      }
    }
    
    
  }

  return quickSort2(left).concat([midValue],quickSort2(right))
}

功能测试

  • 测试splice方式
const arr1 = [1,6,2,4,3,7,5,8,9]

quickSort1(arr1)

结果

[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 测试slice方式
const arr1 = [1,6,2,4,3,7,5,8,9]

quickSort2(arr1)

结果

[1, 2, 3, 4, 5, 6, 7, 8, 9]

单元测试

describe('快速排序', () => {
  it('正常情况', () => {
    const arr = [1, 6, 2, 4, 3, 7, 5, 8, 9]
    const res = quickSort1(arr)
    expect(res).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])

    const arr2 = [1, 6, 2, 4, 3, 7, 5, 8, 9]
    const res2 = quickSort2(arr2)
    expect(res2).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
  })

  it('有负数', () => {
    const arr = [-1, -6, 2, 4, 3, 7, 5, 8, 9]
    const res = quickSort1(arr)
    expect(res).toEqual([-6, -1, 2, 3, 4, 5, 7, 8, 9])

    const arr2 = [-1, -6, 2, 4, 3, 7, 5, 8, 9]
    const res2 = quickSort2(arr2)
    expect(res2).toEqual([-6, -1, 2, 3, 4, 5, 7, 8, 9])
  })

  it('数值一样', () => {
    const arr = [2, 2, 2, 2]
    const res = quickSort1(arr)
    expect(res).toEqual([2, 2, 2, 2])

    const arr2 = [2, 2, 2, 2]
    const res2 = quickSort2(arr2)
    expect(res2).toEqual([2, 2, 2, 2])
  })

  it('空数组', () => {
    const res = quickSort1([])
    expect(res).toEqual([])

    const res2 = quickSort2([])
    expect(res2).toEqual([])
  })
})

性能测试

const test1 = []
for (let i = 0; i < 100 * 10000; i++) {
  const n = Math.floor(Math.random() * 10000)
  test1.push(n)
}
console.time('quickSort1')
quickSort1(test1)
console.timeEnd('quickSort1')

const test2 = []
for (let i = 0; i < 100 * 10000; i++) {
  const n = Math.floor(Math.random() * 10000)
  test2.push(n)
}

console.time('quickSort2')
quickSort2(test2)
console.timeEnd('quickSort2')

打印结果

quickSort1: 713.186ms
quickSort2: 685.652ms

复杂度分析

  • 有遍历,有二分——O(n*logn) 或者O(nlogn)
  • (常规排序,嵌套循环,复杂度是O(n^2))

splice 和 slice 没有区分出来

  • 算法本身的时间复杂度就够高O(n*logn)
  • 外加,splice是逐步 二分 之后执行的,二分会快速削减数量级
  • 如果单独比较splice和slice,效果会非常明显
const test1 = []
for (let i = 0; i < 100 * 10000; i++) {
  const n = Math.floor(Math.random() * 10000)
  test1.push(n)
}

console.time('splice')
test1.splice(50 * 10000 , 1)
console.timeEnd('splice')

const test2 = []
for (let i = 0; i < 100 * 10000; i++) {
  const n = Math.floor(Math.random() * 10000)
  test2.push(n)
}

console.time('slice')
test1.slice(50 * 10000 ,50 * 10000 + 1)
console.timeEnd('slice')

打印结果

splice: 0.657ms
slice: 0.021ms

slice 本身优于splice

总结

  • 常见排序算法
  • 有二分,时间复杂度就包含O(logn)
  • 注意数组操作:splice 和 slice的区别
相关文章
|
JSON 自然语言处理 前端开发
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
626 72
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
|
11月前
|
JavaScript 前端开发 API
|
11月前
|
算法 搜索推荐
快速排序-数据结构与算法
快速排序(Quick Sort)是一种基于分治法的高效排序算法。其核心思想是通过选择基准(pivot),将数组划分为左右两部分,使得左侧元素均小于基准,右侧元素均大于基准,然后递归地对左右两部分进行排序。时间复杂度平均为 O(n log n),最坏情况下为 O(n²)(如数组已有序)。空间复杂度为 O(1),属于原地排序,但稳定性不佳。 实现步骤包括编写 `partition` 核心逻辑、递归调用的 `quickSort` 和辅助函数 `swap`。优化方法有随机化基准和三数取中法,以减少最坏情况的发生。
687 13
|
11月前
|
前端开发 JavaScript 数据可视化
58K star!这个让网页动起来的JS库,前端工程师直呼真香!
Anime.js 是一款轻量级但功能强大的JavaScript动画引擎,它能够以最简单的方式为网页元素添加令人惊艳的动效。这个项目在GitHub上已经获得58,000+星标,被广泛应用于电商页面、数据可视化、游戏开发等场景。
408 8
|
11月前
|
JavaScript 前端开发 容器
|
12月前
|
资源调度 JavaScript 前端开发
前端开发必备!Node.js 18.x LTS保姆级安装教程(附国内镜像源配置)
本文详细介绍了Node.js的安装与配置流程,涵盖环境准备、版本选择(推荐LTS版v18.x)、安装步骤(路径设置、组件选择)、环境验证(命令测试、镜像加速)及常见问题解决方法。同时推荐开发工具链,如VS Code、Yarn等,并提供常用全局包安装指南,帮助开发者快速搭建高效稳定的JavaScript开发环境。内容基于官方正版软件,确保合规性与安全性。
11421 23
|
11月前
|
JavaScript 前端开发
|
11月前
|
存储 JavaScript 前端开发
|
11月前
|
移动开发 JavaScript 前端开发
|
11月前
|
存储 JavaScript 前端开发

热门文章

最新文章