每日一算法:快速排序

作者: lio_zero | 来源:发表于2021-05-06 15:37 被阅读0次

    快速排序是一种最流行的排序算法之一,它的关键在于,快速排序是一种分而治之的算法。如果你不了解,可以查看我之前写的一篇:每日一算法:分治法

    工作原理

    1. 从数列中挑出一个元素,称为 "基准"(pivot);

    2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;

    3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序;

    JavaScript 实现

    使用 quicksort 算法对数字数组进行排序。

    • 使用递归。
    • 使用展开运算符(...)克隆原始数组 arr
    • 如果数组的 length 小于 2,则返回克隆的数组。
    • 使用 Math.floor() 计算轴心元素的索引。
    • 使用 Array.prototype.reduce()Array.prototype.push() 将数组拆分为两个子数组(元素小于或等于 pivot 和大于元素,并将其分解为两个数组)。
    • 递归调用 quickSort() 创建的子数组。
    const quickSort = arr => {
      const a = [...arr]
      if (a.length < 2) return a
      const pivotIndex = Math.floor(arr.length / 2)
      const pivot = a[pivotIndex]
      const [lo, hi] = a.reduce(
        (acc, val, i) => {
          if (val < pivot || (val === pivot && i != pivotIndex)) {
            acc[0].push(val)
          } else if (val > pivot) {
            acc[1].push(val)
          }
          return acc
        },
        [[], []]
      )
      return [...quickSort(lo), pivot, ...quickSort(hi)]
    }
    
    quickSort([1, 6, 1, 5, 3, 2, 1, 4]) // [1, 1, 1, 2, 3, 4, 5, 6]
    

    此示例来自 30 seconds of code 的 quickSort

    更多资料

    白话经典算法系列之六 快速排序 快速搞定

    快速排序 PDF 文档

    QuickSort Tail Call Optimization (Reducing worst case space to Log n )

    Python 的实现方法:Quicksort tutorial: Python implementation with line by line explanation

    相关文章

      网友评论

        本文标题:每日一算法:快速排序

        本文链接:https://www.haomeiwen.com/subject/wtdddltx.html