Asymptotic expansions for moments of number of comparisons

# Asymptotic expansions for moments of number of comparisons used by the randomized quick sort algorithm

Sumit Kumar Jha Center for Security, Theory, and Algorithmic Research
International Institute of Information Technology, Hyderabad, India
###### Abstract.

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.

## 1. Introduction

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

 (1.1) βs(n)=∑k≥0(k)san,kn!,

where

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

 (1.2) βs(n)=2snslogsn+2ss(γ−2)nslogs−1n+O(ns⋅logs−2n),

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:

 Gn(z)=∑k≥0an,kzkn!.
###### Theorem 2.1.

For

 (2.1) Gn(z)=zn−1n∑1≤j≤nGn−j(z)Gj−1(z),

and

 (2.2) G0(z)=1.
###### Proof.

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

 (2.3) an,s=∑1≤k≤n(n−1k−1)∑i+j=s−(n−1)an−k,iak−1,j.

Multiplying equation (2.3) by and dividing by we get

 anszsn!=∑1≤k≤nzsn∑i+j=s−(n−1)an−k,i(n−k)!⋅ak−1,j(k−1)! =∑1≤k≤nzsn⋅{coefficient% of zs−(n−1) in Gn−k(z)⋅Gk−1(z)} =∑1≤k≤nzn−1n⋅zs−(n−1){coefficient of zs−(n−1) in Gn−k(z)⋅Gk−1(z)}

after which summing on gives us equation (2.1). ∎

Now consider the double generating function defined by

 (2.4) H(z,u)=∑n≥0Gn(z)un.
###### Corollary 2.2.

We have

 (2.5) ∂H(z,u)∂u=H2(z,zu),

and

 (2.6) H(1,u)=(1−u)−1.
###### Proof.

From equation (2.1) we have

 ∂H(z,u)∂u=∑n≥1(uz)n−1∑1≤j≤nGn−j(z)Gj−1(z) =∑n≥1(uz)n−1⋅{coefficient of (uz)n−1 in H(z,uz)⋅H(z,uz)} =H(z,zu)⋅H(z,zu).

Equation (2.6) follows from the fact that . ∎

Now notice that

 (2.7) βs(n)=[dsdzsGn(z)]z=1.

The generating functions of are

 (2.8) fs(u)=∑n≥0βs(n)un.

Using Taylor’s formula and equation (2.7) we can write

 (2.9) H(z,u)=∑s≥0fs(u)(z−1)ss!.
###### Theorem 2.3.

For integer we have

 (2.10) f′s(u)=s!⋅∑j+k+l+m=sf(k)j(u)⋅f(m)l(u)⋅uk+mj!⋅k!⋅l!⋅m!,
###### Proof.

Using Taylor’s theorem we can write

 fj(x)=∑k≥0f(k)j(u)(x−u)kk!

which on substituting gives

 (2.11) fj(uz)=∑k≥0f(k)j(u)(z−1)kukk!.

Now substituting equation (2.9) in equation (2.5) gives:

 ∑s≥0f′s(u)(z−1)ss!=∑p≥0fp(uz)(z−1)pp!⋅∑r≥0fr(uz)(z−1)rr! =∑p≥0(z−1)pp!∑l≥0f(l)p(u)(z−1)lull!⋅∑r≥0(z−1)rr!∑m≥0f(m)r(u)(z−1)mumm! =∑h≥0(z−1)h∑j+k+l+m=h1j!⋅f(k)j(u)ukk!⋅1l!⋅f(m)l(u)umm!

Now comparing coefficients on both sides of the equation gives

 f′s(u)=s!⋅∑j+k+l+m=sf(k)j(u)⋅f(m)l(u)⋅uk+mj!⋅k!⋅l!⋅m!.

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 .

###### Corollary 2.4.

For integers we have

 (2.12) fs(u)=2ss!Ls(u)(1−u)s+1+s(Hs−2)2ss!Ls−1(u)(1−u)s+1+Rs−2,s+1(u),

where .

###### Proof.

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:

 (2.13) f′s(u)−21−ufs(u)=ps(u)

where

 (2.14) ps(u)=s!⋅∑j+k+l+m=sj,l≠sf(k)j(u)⋅f(m)l(u)⋅uk+mj!⋅k!⋅l!⋅m!.

Solving the linear differential equation (2.13) gives us

 (2.15) fs(u)=1(1−u)2∫u0ps(t)⋅(1−t)2dt.

In the following calculation we quite frequently use the fact that the derivatives of

 g(u)=d⋅q!⋅Lp(u)(1−u)−q−1+Rp−1,q+1(u)(% d being a constant)

satisfy

 g(i)(u)=d⋅(q+i)!⋅Lp(u)⋅(1−u)−q−i−1+Rp−1,q+i+1(u).

Specially for we have

 f(i)j(u)=2j⋅(j+i)!⋅Lj(u)⋅(1−u)−j−i−1+Rj−1,j+i+1(u).

After equation (2.14) we have

 ps(u)=s!⋅∑j+l=sj,l≠sfj(u)⋅fl(u)j!⋅l!+2⋅s!⋅∑j+l=s−1u⋅f′j(u)⋅fl(u)j!⋅l!+Rs−2,s+2(u) =2s⋅s!⋅∑j+l=sj,l≠s{(Lj(u)(1−u)j+1+j(Hj−2)Lj−1(u)(1−u)j+1)⋅(Ll(u)(1−u)l+1+l(Hl−2)Ll−1(u)(1−u)l+1)} +2s⋅s!∑j+l=s−1{u⋅(j+1)Lj(u)(1−u)j+2⋅Ll(u)(1−u)l+1}+Rs−2,s+2(u) =2ss!(1−u)s+2{(s−1)Ls(u)+(s(s+1)2+2s−1∑k=1k⋅(Hk−2))Ls−1(u)}+Rs−2,s+2(u).

Thereafter equation (2.15) gives

 fs(u)=2s⋅s!(1−u)2∫u01(1−t)s{(s−1)Ls(t)+(s(s+1)2+2s−1∑k=1k⋅(Hk−2))Ls−1(t)}dt +Rs−2,s+1(u) =2ss!Ls(u)(1−u)s+1+(s2+2s−1s−1∑k=1k⋅(Hk−2))2ss!Ls−1(u)(1−u)s+1+Rs−2,s+1(u) =2ss!Ls(u)(1−u)s+1+s⋅(Hs−2)2ss!Ls−1(u)(1−u)s+1+Rs−2,s+1(u)

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]).

Let

 (2.16) fα,β(u)≡f(u)=1(1−u)α(log11−u)β,

where is a positive integer and is a non-negative integer. The coefficient of in , denoted , admits the asymptotic expansion

 (2.17) [un]f(u)∼nα−1(α−1)!(logn)β[1+C11!βlogn+C22!(β)(β−1)log2(n)+⋯],

where

 Ck=(α−1)![dkdxk1Γ(x)]x=α

and is the gamma function.

We can use the above result to conclude that

 [un]{2s⋅s!⋅Ls(u)(1−u)s+1}=2sns(logsn+s⋅C1⋅logs−1n+O(logs−2n)),
 [un]{2s⋅s!⋅Ls−1(1−u)s+1}=2sns(logs−1n+O(logs−2n)).

Here

 C1=s!⋅[ddx1Γ(x)]x=s+1=−s!Γ(s+1)⋅Γ′(s+1)Γ(s+1)=−(Hs−γ).

Here we used the result where is the digamma function [digamma]. Now we can conclude our result after equation (2.12). ∎

## References

You are adding the first comment!
How to quickly get a good reply:
• Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
• Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
• Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters   