# Asymptotic expansions for moments of number of comparisons

used by the randomized quick sort algorithm

###### 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) |

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

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:

###### Theorem 2.1.

For

(2.1) |

and

(2.2) |

###### 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) |

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

(2.4) |

###### Corollary 2.2.

We have

(2.5) |

and

(2.6) |

Now notice that

(2.7) |

The generating functions of are

(2.8) |

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

(2.9) |

###### Theorem 2.3.

For integer we have

(2.10) |

###### Proof.

Using Taylor’s theorem we can write

which on substituting gives

(2.11) |

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

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

where

(2.14) |

Solving the linear differential equation (2.13) gives us

(2.15) |

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

satisfy

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

Let

(2.16) |

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

(2.17) |

where

and is the gamma function.

We can use the above result to conclude that

Here

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