关于Java:双重枢纽快速排序和快速排序有什么区别?

What's the difference of dual pivot quick sort and quick sort?

我以前从未见过双重枢纽快速排序。
如果是快速排序的升级版本?
双重枢纽快速分类和快速分类有什么区别?


我在Java文档中找到了这个。

The sorting algorithm is a Dual-Pivot Quicksort
by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
offers O(n log(n)) performance on many data sets that cause other
quicksorts to degrade to quadratic performance, and is typically
faster than traditional (one-pivot) Quicksort implementations.

然后,我在Google搜索结果中找到了这个。
快速排序算法简介:

  • Pick an element, called a pivot, from the array.
  • Reorder the array so that all elements, which are less than the pivot, come before the
    pivot and all elements greater than the pivot come after it (equal values can go
    either way). After this partitioning, the pivot element is in its final position.
  • Recursively sort the sub-array of lesser elements and the sub-array of greater elements.
  • 相比之下,双轴快速排序:

    (Illustration)

  • For small arrays (length < 17), use the Insertion sort algorithm.
  • Choose two pivot elements P1 and P2. We can get, for example, the first element
    a[left] as P1 and the last element a[right] as P2.
  • P1 must be less than P2, otherwise they are swapped. So, there are the following parts:

    • part I with indices from left+1 to L–1 with elements, which are less than P1,
    • part II with indices from L to K–1 with elements, which are greater or equal to P1 and less or equal to P2,
    • part III with indices from G+1 to right–1 with elements greater than P2,
    • part IV contains the rest of the elements to be examined with indices from K to G.
  • The next element a[K] from the part IV is compared with two pivots P1 and P2,
    and placed to the corresponding part I, II, or III.
  • The pointers L, K, and G are changed in the corresponding directions.
  • The steps 4 - 5 are repeated while K ≤ G.
  • The pivot element P1 is swapped with the last element from part I,
    the pivot element P2 is swapped with the first element from part III.
  • The steps 1 - 7 are repeated recursively for every part I, part II, and part III.

  • 对于那些感兴趣的人,看看他们如何在Java中实现此算法:

    http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/DualPivotQuicksort.java#DualPivotQuicksort.sort%28int%5B%5D%2Cint%2Cint% 2Cint%5B%5D%2Cint%2Cint%29

    如来源所述:

    "如果可能的话,使用给定的工作区数组切片对数组的指定范围进行排序

    该算法可在许多数据集上提供O(n log(n))性能,这会导致其他快速排序降级为二次性能,并且通常比传统的(单轴)Quicksort实现要快。"


    我只是想从算法的角度来补充一点(即,成本仅考虑比较和交换的次数),如果没有,2-pivot quicksort和3-pivot quicksort不会比传统的quicksort(使用1个枢轴)更好。更差。
    但是,由于它们利用了现代计算机体系结构的优势,因此它们在实践中速度更快。具体而言,它们的高速缓存未命中的数量较小。因此,如果我们删除所有缓存,并且只有CPU和主内存,就我的理解而言,2 / 3-pivot快速排序比传统的快速排序更糟糕。

    参考文献:
    三轴快速排序:https://epubs.siam.org/doi/pdf/10.1137/1.9781611973198.6
    分析它们为何比传统Quicksort更好的原因:https://arxiv.org/pdf/1412.0193v1.pdf
    完整且不太详细的参考:https://algs4.cs.princeton.edu/lectures/23Quicksort.pdf