Sorting data is one of the most fundamental problems in Computer Science, especially if the arranging objects are primitive ones, such as integers, bytes, floats, etc. Since sorting methods play an important role in the operation of computers and other data processing systems, there has been an interest in seeking new algorithms better than the existing ones. We compare sorting methods by the number of the most “expensive” operations, which influence on effectiveness of the sorting techniques, — comparisons and swaps. Quicksort algorithm is an effective and wide-spread sorting procedure with C×n×ln(n) operations, where n is the size of the arranged array. The problem is to find an algorithm with the least coefficient C . There were many attempts to improve the classical variant of the Quicksort algorithm:
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.
Hoare, Helman, Knuth, Sedgewick, Bentley and other scientists worked mostly on the effectiveness of the concrete “divide and conquer” algorithm implementations, or tried to increase performance due to the specific choice of the pivot element, but all of them used the classical partitioning scheme with two parts.
We can show that using two pivot elements (or partitioning to three parts) is more effective, especially on large arrays. We suggest the new Dual-Pivot Quicksort scheme, faster than the known implementations, which improves this situation. The implementation of the Dual-Pivot Quicksort algorithm has been investigated on different inputs and primitive data types. Its advantages in comparison with one of the most effective known implementations of the classical Quicksort algorithm [1], [2], and implementation of Quicksort in JDK™ 6.0 platform [3] have been shown.
New Dual-Pivot Quicksort algorithm
The new Quicksort algorithm uses partitioning a source array T[] a, where T is primitive type (such as int, float, byte, char, double, long and short), to three parts defined by two pivot elements P1 and P2 (and therefore, there are three pointers L, K, G, and left and right — indices of the first and last elements respectively) shown in Figure 1:
The algorithm provides the following steps:
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.
Mathematical proof
It is proved that for the Dual-Pivot Quicksort the average number of comparisons is 2×n×ln(n) , the average number of swaps is 0.8×n×ln(n) , whereas classical Quicksort algorithm has 2×n×ln(n) and 1×n×ln(n) respectively.
At first consider the classic Quicksort scheme and find the average number of comparisons and swaps for it. We assume that input data is random permutation of n numbers from the range [1…n] .
Classic Quicksort
Choose a pivot element (take random),
Compare each (n−1 ) elements with the pivot and swap it, if necessary, to have the partitions: [ <= pivot | >= pivot ]
Sort recursively left and right parts.
From the algorithm above, the average number of comparisons Cn as a function of the number of elements may be represented by the equation:
Cn=(n−1)+n1×k=0∑n−1(Ck+Cn−k−1)(1)
and last sum can be rewritten:
Cn=(n−1)+n2×k=0∑n−1Ck(2)
Write formula (2) for n+1 :
Cn+1=n+(n+1)2×k=0∑nCk(3)
Multiply (2) by n and (3) by (n+1) and subtract one from other, we have:
(n+1)×Cn+1−n×Cn=2×n+2×Cn(4)
Sorting an array of n elements may be considered as selecting one permutation of the n elements among all possible permutations. The number of possible permutations of n elements is n! , so the task for any sorting algorithm is to determine the one permutation out of n! possibilities. The minimum number of operations (swap and comparisons) for sorting n elements is const×ln(n!) . From the Stirling’s formula the approximation of the number of operations is A×n×ln(n)+B×n+C , where A , B and C are constant coefficients. The coefficients B and C are not important for large n . Therefore, the function Cn may be approximated by the equation:
Cn=A×n×ln(n)(5)
The function Cn is substituted from equation (5) into equation (4), which yields the following equation:
Using the properties of logarithms, equation (6) can then be reduced to:
n×ln(1+n1)+2×ln(1+n1)+n1×ln(n+1)=A2(7)
Using a property of logarithm: ln(1+x)→x , if x→0 , and other property: nln(n)→0 , when n→+∞ , equation (7) will be approximated by:
1+0+0=A2(8)
So, the coefficient A is equal to 2 and the average number of comparisons in sorting of n size arrays is
Cn=2×n×ln(n)(9)
To find the approximation of the average number of swaps, we use the similar approach as in the case of comparisons. The average number of swaps Sn as a function of the number of elements may be represented by the equation:
Sn=21×(n−1)+n2×k=0∑n−1Sk(10)
We assume that average number of swaps during one iteration is 21×(n−1) . It means that in average one half of elements is swapped only. Using the same approach, we find that the coefficient A equals to 1. Therefore, the function Sn may be approximated by the equation:
Sn=n×ln(n)(11)
Now consider the Dual-Pivot Quicksort scheme and find the average number of comparisons and swaps for it. We assume that input data is random permutation of n numbers from the range [1…n] .
Dual-Pivot Quicksort
Choose 2 pivot elements pivot1 and pivot2 (take random),
pivot1 must be less or equal than pivot2, otherwise they are swapped
Compare each (n−2 ) elements with the pivots and swap it, if necessary, to have the partitions: [ <= p1 | p1 <= & <= p2 | >= p2 ]
Sort recursively left, center and right parts.
From the algorithm above, the average number of comparisons Cn as a function of the number of elements may be represented by the equation:
Equation (1) means that total number is the sum of the comparison numbers of all cases of partitions into 3 parts plus number of comparisons for elements from left part (one comparison), center and right parts (2 comparisons).
We will show that n×(n−1)2×∑i=0n−2∑j=i+1n−1[i+2×(j−i−1)+2×(n−j−1)] equals to35×(n−2) . Let’s consider the double sum:
Using a property of logarithm: ln(1+x)→x , if x→0 , and other property: nln(n)→0 , when n→+∞ , equation (15) will be approximated by:
−1+4+2+0+0=A10(15)
So, the coefficient A is equal to 2 and the average number of comparisons in sorting of n size arrays is
Cn=2×n×ln(n)(16)
To find the approximation of the average number of swaps, we use the similar approach as in the case of comparisons. The average number of swaps Sn as a function of the number of elements may be represented by the equation:
We assume that average number of swaps during one iteration is 32×(n−2) . It means that in average one third of elements is swapped only. Using the same approach, we find that the coefficient A equals to 0.8 . Therefore, the function Sn may be approximated by the equation:
Sn=0.8×n×ln(n)(18)
And as summary: the value of the coefficient A :
dual-pivot
classic
comparison:
2.0
2.0
swap:
0.8
1.0
Comparison and summary
The new Dual-Pivot Quicksort algorithm provides the following advantages:
While sorting primitive objects, it is more efficient to use partitioning of unsorted array to 3 parts instead of the usage of the classical approach. The more the size of the array to be sorted, the more efficiently the new algorithm works in comparison with the classical Quicksort [2] and the Quicksort implemented in JDK 6.0 platform [3]. For example, we provided the following numerical experiment: 2 000 000 random integer elements were sorted 50 times using the new Dual-Pivot Quicksort, algorithms [2] and [3], and analyzed the calculation time. It took 16.5, 18.9, and 20.3 seconds respectively. The implementation of the new Dual-Pivot Quicksort algorithm for integers can be easy adjusted for another numeric, string and comparable types.
The suggested Dual-Pivot Quicksort algorithm also works quicker than the classical schemes on the arranged arrays or the arrays with repeated elements. In these cases of nonrandom inputs the time metric for the Dual-Pivot Quicksort algorithm is 55 against 100 for Quicksort implemented in JDK 6.0 platform, where all tests have been run on.
The supposed algorithm has additional improvement of the special choice procedure for pivot elements P1 and P2. We take not the first a[left] and last a[right] elements of the array, but choose two middle elements from 5 middle elements. The described modification does not make worse the properties of the Dual-Pivot Quicksort algorithm in the case of random source data.
We can summarize the features of the suggested algorithm as follows:
Time savings.
The “divide and conquer” strategy.
Usage of two pivot elements instead of one.
More effective sorting procedure that can be used in numerical analysis.
The Dual-Pivot Quicksort algorithm could be recommended in the next JDK releases.
Implementation in Java™ programming language
Experimental results
The a lot of tests have been run, here is the time of executions: