Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've never seen dual pivot quick sort before. Is it an upgraded edition of quick sort?
And what is the difference between dual pivot quick sort and quick sort?

like image 626
Brutal_JL Avatar asked Jan 04 '14 06:01

Brutal_JL


People also ask

Why is dual pivot quicksort better?

Dual pivot quick sort is a little bit faster than the original single pivot quicksort. But still, the worst case will remain O(n^2) when the array is already sorted in an increasing or decreasing order.

How good is multi pivot quicksort?

The algorithm behaves best if 3 to 5 pivots are used. Experiments show that this translates into good cache behavior and is closest to predicting observed running times of multi-pivot quicksort algorithms. Finally, it is studied how choosing pivots from a sample affects sorting cost.

What is quick sort pivot?

The quicksort algorithm is a sorting algorithm that sorts a collection by choosing a pivot point, and partitioning the collection around the pivot, so that elements smaller than the pivot are before it, and elements larger than the pivot are after it.


2 Answers

I find this in the Java doc.

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.

Then I find this in the Google search result. Theory of quick sort algorithm:

  1. Pick an element, called a pivot, from the array.
  2. 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.
  3. Recursively sort the sub-array of lesser elements and the sub-array of greater elements.

In comparison, dual-pivot quick sort:

Illustration

  1. For small arrays (length < 17), use the Insertion sort algorithm.
  2. 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.
  3. 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.
  4. 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.
  5. The pointers L, K, and G are changed in the corresponding directions.
  6. The steps 4 - 5 are repeated while K ≤ G.
  7. 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.
  8. The steps 1 - 7 are repeated recursively for every part I, part II, and part III.
like image 123
Brutal_JL Avatar answered Oct 02 '22 19:10

Brutal_JL


I just want to add that from the algorithm point of view (i.e. the cost only considers the number of comparisons and swaps), 2-pivot quicksort and 3-pivot quicksort is not better than classical quicksort (which uses 1 pivot), if not worse. However, they are faster in practice since they take the benefits of modern computer architecture. Specifically, their numbers of cache misses are smaller. So if we remove all caches and there are only CPU and main memory, in my understanding, 2/3-pivot quicksort is worse than classical quicksort.

References: 3-pivot Quicksort: https://epubs.siam.org/doi/pdf/10.1137/1.9781611973198.6 Analysis of why they perform better than classical Quicksort: https://arxiv.org/pdf/1412.0193v1.pdf A complete and not-too-much-details reference: https://algs4.cs.princeton.edu/lectures/23Quicksort.pdf

like image 45
Duong Nguyen Avatar answered Oct 02 '22 20:10

Duong Nguyen