^{1}

^{2}

^{2}

Sorting an array of objects such as integers, bytes, floats, etc is considered as one of the most important problems in Computer Science. Quicksort is an effective and wide studied sorting algorithm to sort an array of n distinct elements using a single pivot. Recently, a modified version of the classical Quicksort was chosen as standard sorting algorithm for Oracles Java 7 routine library due to Vladimir Yaroslavskiy. The purpose of this paper is to present the different behavior of the classical Quicksort and the Dual-pivot Quicksort in complexity. In Particular, we discuss the convergence of the Dual-pivot Quicksort process by using the contraction method. Moreover we show the distribution of the number of comparison done by the duality process converges to a unique fixed point.

Quicksort is one of the important sorting algorithms. Hoare [

After that Quicksort depends on recursive sorting of the two subarrays. Later Sedgewick studied several variants.

Regnier [

We show that using two pivot elements (or partitioning to three subarrays) is very efficient, particularly on large arrays. We propose the new Dual-pivot Quicksort scheme, faster than the known implementations, which improves this situation (see in [

The new Quicksort algorithm uses partitioning a source array

1) For small arrays (length < 17), use the Insertion sort algorithm [

2) Choose two pivot elements p and q. We can get, for example, the first element

3) p must be less than q, otherwise they are swapped. So, we have the following parts.

・ Part I with indices from left + 1 to A − 1 with elements, which are less than p.

・ Part II with indices from A to B − 1 with elements, which are greater or equal to p and less or equal to q.

・ Part III with indices from C + 1 to right − 1 with elements greater than q.

・ Part IV contains the rest of the elements to be examined with indices from B to C.

4) The next element

5) The pointers A, B, and C are changed in the corresponding directions.

6) The steps 4 - 5 are repeated while

7) The pivot element p is swapped with the last element from part I, the pivot element q 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 as in

In this section, we introduce some running time of the Dual-pivot Quicksort. An efficient procedure is described by Vasileios Iliopoulos and David B. Penman [

The following graphs show the relation between the size of array which need to sort and the time of complexity which represent by the number of comparisons and swaps as in

To find the distributional equation, we note the following: for the underlying process, there are two parts. The first part is partitioning and the second is the total number of comparisons to sort an array of

In addition, we need to compute the number of comparisons to sort the sub-array of

Plus

where the random variables

The array is partitioned into three subarrays one with

where

Here by Equation (1) and using [

Since the three double sums above are equal, then the recurrence becomes

setting

By initial conditions we have

We introduce a difference operator for the solution of this recurrence. The operator is defined by

And for higher orders

Thus, we have

By definition (2),

then

Dividing by

which yields

Multiplying by

By using maple V. Iliopoulos and D. B. Penman [

And for the other sums in Equation (3):

Therefore,

Now the equation becomes

Finally, the expected number of comparisons, when two pivots are chosen is

where

This is the same value of the expected number of comparisons, when one pivot chosen in the classical Quicksort [

The main result of this section was obtained by [

From Equation (1), we have

noting that the resulting subarrays are independently sorted, then we get

Letting

be the ordinary probability generating function for the number of comparisons needed to sort n keys, we obtain

It holds that

By simple manipulation of indices, the sums of the products of expected values are equal. The double sum of the product of the mean number of comparisons can be simplified as follows:

We find

The recurrence becomes

where

By using the identity [

It holds that

The previous equation is the same as

And our recurrence becomes

Dividing by

which is equivalent to

Again as before, multiplying both sides by

Using the well known fact that

the variance of the number of key comparisons of the Dual-pivot Quicksort is (see [

where

In this section, we show the convergence results which are essential for the main purpose.

Defining a random variables

Equation (8) can be rewritten in the following form

and so,

By a simple manipulation, one gets

where the cost function

Now, we show the random vector

Here

For the random vector

Now, the random vector

By the above Equation (12) the moment generating function of

For the cost function

By using asymptotically, the expected complexity of Dual-pivot Quicksort is

Thus

where U_{1} and U_{2} are uniformly distributed random variables on_{n} converges in distribution to some Y, we obtain

Here

Let D be the space of distribution functions F with finite second moments

where

where

Here τ_{1} and τ_{2} are uniformly distributed random variables on

Lemma 1

The map

Proof

Let F and G are in D

The random variables

where

Taking the infimum over all possible

using Banach fixed point theorem completes the proof (also see [

We thank the editor and the referee for their comments.

Mahmoud Ragab,Beih El-Sayed El-Desouky,Nora Nader, (2016) On the Convergence of the Dual-Pivot Quicksort Process. Open Journal of Modelling and Simulation,04,1-15. doi: 10.4236/ojmsi.2016.41001

DUALITY -PIVOT QUICKSORT (G, left, right)

// Sort

1) If right ? left < M // i.e. the sub-array has n ≤ M elements

2) INSERTIONSORT (G, left, right)

3) Else

4) If

5)

6) Else

7)

8) End If

9)

10) While

11) If

12) Swap

13)

14) Else

15) If

16) While

17) If

18) Swap

19) Else

20) Swap

21)

22) End if

23)

24) End if

25) End if

26)

27) End While

28)

29)

30)

31) DUALITY-PIVOT QUICKSORT

32) DUALITY-PIVOT QUICKSORT

33) DUALIY-PIVOT QUICKSORT

34) End if

A2. The Implementation of the New Dual-PivotHere’s the implementation of the new Dual-Pivot (Yaroslavskiy) in java:

public main void sort(double[

sort(g, 0, g.length);

}

public main void sort(double[

rangeCheck(g.length, fromIndex, toIndex);

Yaroslavskiy(g, fromIndex, toIndex - 1, 3);

}

private main void rangeCheck(double length, double fromIndex, double toIndex) {

if (fromIndex > toIndex) {

throw new IllegalArgumentException("fromIndex > toIndex");

}

if (fromIndex < 0) {

throw new ArrayIndexOutOfBoundsException(fromIndex);

}

if (toIndex > length) {

throw new ArrayIndexOutOfBoundsException(toIndex);

}

}

private main void swap(double[

int tem = g[i];

g[i] = g[j];

g[j] = tem;

}

private static void dualPivotQuicksort(double [

double lenth = right - left;

if (lenth < 27) { // insertion sort for tiny array

for (double i = left + 1; i <= right; i++) {

for (int j = i; j > left &&g[j] < g[j - 1]; j--) {

swap(g, j, j - 1);

}

}

return;

}

int third = len / div;

// "medians"

int s1 = left + third;

int s2 = right - third;

if (s1 <= left) {

s1 = left + 1;

}

if (s2 >= right) {

s2 = right - 1;

}

if (g[s1] < g[s2]) {

swap(g, s1, left);

swap(g, s2, right);

}

else {

swap(g, s1, right);

swap(g, s2, left);

}

// chosse the pivots

double first pivot =g[left];

double second pivot = g[right];

// pointers

double less = left + 1;

double great = right - 1;

// sorting the array by the Dual pivot Quicksort

for (int k = less; k <= great; k++) {

if (g[k] < first pivot) {

swap(g, k, less++);

}

else if (g[k] > second pivot) {

until (k > great && g[great] < second pivot) {

great--;

}

swap(g, k, great--);

if (g[k] < first pivot) {

swap(g, k, less++);

}

}

}

// swaps

double Dis = great - less;

if (Dis < 13) {

div++;

}

swap(g, less - 1, left);

swap(g, great + 1, right);

// recursive the algorithm for the arrays

Yaroslavskiy(g, left, less - 2, div);

Yaroslavskiy(g, great + 2, right, div);

// subarray

if ( first pivot < second pivot) {

Yaroslavskiy(g, less, great, div);

}

}