Asymptotic expansions for moments of number of comparisons
used by the randomized quick sort algorithm
We calculate asymptotic expansions for the moments of number of comparisons used by the randomized quick sort algorithm using the singularity analysis of certain generating functions.
The basic quick sort algorithm recursively sorts numbers in an array by partitioning the input array into two independent and smaller subarrays, and thereafter sorting these subarrays. The partitioning procedure chooses the first or last element of the array as pivot and puts it in its right place so that numbers to the left of it are smaller than it, and those appearing to the right of it are larger than it.
For purposes of this analysis we assume that the input array to the quick sort algorithm contains distinct numbers. Since the sorting algorithm depends only on the relative order of the numbers in the input array and not their individual values, therefore we can associate to each input array having elements a permutation of .
Let denote the set of all permutations of . We assume that input arrays of size are permutations in the set with uniform probability distribution (each permutation occurring with probability .)
Let be the number of permutations in requiring a total of comparisons to sort by the quick sort algorithm. Define th factorial moment of the number of comparisons by
We wish to calculate the following important result using the singularity analysis of certain generating functions appearing in [flajolet]
Theorem 1.1 ([bworld]).
For integers we have
where is the Euler’s constant.
Note that the th moment of number of comparisons admits the same asymptotic expansion as since it is just a linear combination of th factorial moments for .
2. The Calculation
We start be defining the probability generating function:
We assume that the first partitioning stage requires comparisons (for some other variants this might be which would result in the same asymptotic expression).
Fix some such that . Let denote the set of all permutations in having the pivot element equal to .
Each element , after the partitioning procedure executed on it, produces a pair where and denote the permutations associated with the sub arrays of sizes and obtained after the partitioning procedure is applied on , respectively.
We further assume that the partitioning method preserves equally likely distribution on produced permutations, that is, every pair in should get produced from partitioning of the same number of permutations in , that is, [hennequin]. Thus we can write
Multiplying equation (2.3) by and dividing by we get
after which summing on gives us equation (2.1). ∎
Now consider the double generating function defined by
Now notice that
The generating functions of are
Using Taylor’s formula and equation (2.7) we can write
For integer we have
Using Taylor’s theorem we can write
which on substituting gives
Notation: In the following let Further let be an unspecified linear combination of terms of the form where are integers with either and is arbitrary, or and .
For integers we have
We prove this by induction on . The base case when is trivial since . Now assume that the assertion is true for all integers . First observe that the equation (2.10) is the following linear differential equation:
Solving the linear differential equation (2.13) gives us
In the following calculation we quite frequently use the fact that the derivatives of
Specially for we have
After equation (2.14) we have
Thereafter equation (2.15) gives
where to deduce the last step we used the identity and to deduce the second last step we used integration by parts. ∎
Proof of Theorem 1.1.
We would use the following result to conclude the assertion:
Theorem 2.5 (Flajolet and Odlyzko [flajolet]).
where is a positive integer and is a non-negative integer. The coefficient of in , denoted , admits the asymptotic expansion
and is the gamma function.
We can use the above result to conclude that
Here we used the result where is the digamma function [digamma]. Now we can conclude our result after equation (2.12). ∎