probabilistic analysis of sorting algorithms lecture …phitczen/lectnotes.pdflent monograph on the...

68
Probabilistic Analysis of Sorting Algorithms Lecture Notes Pawe l Hitczenko * Department of Mathematics Drexel University Philadelphia, PA 19104, U.S.A. email: [email protected] * supported in part by NSA grant MSPF-02G-043 1

Upload: others

Post on 21-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Probabilistic Analysis of Sorting Algorithms

Lecture Notes

PaweÃl Hitczenko∗

Department of MathematicsDrexel University

Philadelphia, PA 19104, U.S.A.email: [email protected]

∗supported in part by NSA grant MSPF-02G-043

1

Page 2: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

1 Introduction

The following are lecture notes to a course ”Probabilistic Analysis of SortingAlgorithms” that I gave as a special topics course at Drexel University in thesummer of 2002, and at the University of Jyvskyl, Finland, in the fall of thesame year. The original intent of the course was to cover large parts of an excel-lent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: ADistribution Theory, Wiley 2000. It turned out, however, that his text assumesa solid knowledge of a graduate level probability theory, a rather demanding as-sumption for most of Drexel’s (and, as it turned out, the University of Jyvskyl’s)students potentially interested in such a course. As a consequence, almost halfof a 10 week long course at Drexel was spent on developing necessary tools fromthe probability theory (the ultimate goal was the central limit theorem). Thenotes reflect that: the first part consists of a brief introduction to probabil-ity theory. The development was based largely on an excellent monograph byP. Billingsley Probability and Measure, Wiley (1995), 3rd Ed. (A.N. Shiryaev’sProbability, Springer (1995), 2nd Ed. is a good alternative). It should beemphasized, however, that the notes are far from a comprehensive treatmentof even basic probability; the goal was to get to the central limit theorem asquickly as possible. Either of the texts just mentioned or, on a non–measuretheoretic level, books like S. Ross, A First Course in Probability, Macmillan,or M. H. DeGroot, Probability and Statistics, Addison–Wesley, should be con-sulted for a much more complete development. The second part of the notes isclosely based on selections from Mahmoud’s Sorting. Again, we would like toemphasize, that because of a very limited time left for the algorithmic part, theselections are very constrained and even for the topics included here, discussionsrather brief. Thus, the original text of Mahmoud’s book (or D.E. Knuth’s 3rdvolume of The Art of Computer Programming. Sorting and Searching, Addison–Wesley (1973)) should be consulted for a much more detailed analysis. Since Iwill refrain from providing historical references and credits in the text, I referto Mahmoud’s texts for references (up to, roughly, 2000). As one exception Iwould like to mention a paper H.–K. Hwang and R. Neininger ”Phase changeof limit laws in the quicksort recurrence under varying toll functions” SIAM J.Computing (2003), 1475–1501, which contains a quite complete, and perhaps,essentially final discussion of quicksort type of recurrences.

Someone to wishes to learn more about general methods used in discretemathematics (they are used throughout the notes in a very limited way), willfind plenty of methods in Concrete Mathematics: A Foundation for ComputerScience, Addison–Wesley (1989).

As I mentioned above, one occasion on which this course was taught, wasduring fall 2002 semester, which I spent as a visiting professor at the Universityof Jyvskyl, Jyvskyl, Finland. I would like to thank the Department of theMathematics and Statistics of the University of Jyvskyl for the invitation andto Christel and Stefan Geiss for their hospitality during that visit.

Finally, I would like to thank Rainer Avikainen for his help with final prepa-ration of the notes in June of 2003.

2

Page 3: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

2 Complex Numbers

Real numbers, as good as they are, do not suffice for all purposes. For example,some very simple polynomials do not necessarily have real roots. To bypass thatproblem we need to consider more general system of numbers, namely complexnumbers. This is done by introducing the number i, called an imaginary unit,by letting i =

√−1 (or by saying that i2 = −1). We then define a complexnumber z by

z = a + bi,

where a and b are real numbers. These are referred to as the real and imaginarypart of z, respectively, and are denoted by a = <(z), and b = =(z). A complexconjugate of z is a complex number z defined by z = a−bi. The absolute value ofz is a nonnegative number defined by |z| = √

a2 + b2. Arithmetic operations oncomplex numbers are defined as follows: addition and subtraction is coordinate-wise

z1 ± z2 = (a1 ± a2) + (b1 ± b2)i,

and multiplication by expanding the product

z1z2 = (a1 + b1i)(a2 + b2i) = a1a2 + b1b2i2 + (a1b2 + a2b1)i

= (a1a2 − b1b2) + (a1b2 + a2b1)i.

Division is performed by first removing imaginary part from the denominator;for any complex number z = a + bi we have

zz = (a + bi)(a− bi) = a2 + b2 = |z|2.Hence,

a1 + b1i

a2 + b2i=

(a1 + b1i)(a2 − b2i)(a2 + b2i)(a2 − b2i)

=(a1a2 + b1b2) + (b1a2 − a1b2)i

a22 + b2

2

=a1a2 + b1b2

a22 + b2

2

+ ib1a2 − a1b2

a22 + b2

2

.

We will need a complex valued function of a real variable, namely, x → eix.The following provides heuristics behind the definition: consider Taylor seriesof ey with y = ix. By grouping together even and odd powers and taking intoaccount the defining property of i we get

eix =∞∑

n=0

(ix)n

n=

∞∑

k=0

(ix)2k

(2k)+

∞∑

k=0

(ix)2k+1

(2k + 1)

=∞∑

k=0

(−1)kx2k

(2k)+ i

∞∑

k=0

(−1)kx2k+1

(2k + 1).

We now notice that the first sum is just the Taylor series of cos x while thesecond is the that of sin x. In view of this we set: the function x → eix isdefined by

eix = cos x + i sinx.

3

Page 4: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

The following lemma lists the basic properties of eix:

Lemma 2.1 We have

(i) |eix| = 1

(ii) eix · eiy = ei(x+y)

(iii) (eix)′ = ieix

(iv)∫

eixdx = −ieix

Proof: We begin with (ii). Using the formulas for the cosine and sine of the sumof two angles, we see that:

ei(x+y) = cos(x + y) + i sin(x + y)= cos x cos y − sin x sin y + i(sinx cos y + sin y cos x)= cos x cos y + i2 sinx sin y + i(sinx cos y + sin y cos x)= (cos x + i sinx)(cos y + i sin y)= eix · eiy

as required. Part (i) follows from (ii), the fact that |z| = √zz, and that

eix = cos x− i sin x = cos(−x) + i sin(−x) = e−ix.

For part (iii) write

(eix)′ = (cos x + i sin x)′ = − sinx + i cos x = i2 sin x + i cos x

= i(cos x + i sin x) = ieix.

Finally, (iv) follows from (iii) and the fact that that 1/i = −i. ¤

3 Probability Space

Let Ω be a set. A family A of subsets of Ω is called a σ–algebra (or a σ–field)of sets if it satisfies the following conditions:

• Ω ∈ A,

• for every set A, if A ∈ A, then its complement Ac is in A,

• if A1, A2, . . . is a sequence such that ∀i ≥ 1 A ∈ A then their union⋃∞j=1 Aj ∈ A.

A triple (Ω,A,P) is called a probability space if Ω is a set, A is a σ–field ofsubsets of Ω, and P is a function defined on A satisfying the following conditions(axioms of probability):

• ∀A ∈ A, P(A) ≥ 0,

4

Page 5: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

• P(Ω) = 1,

• P(⋃∞

j=1 Aj) =∑∞

j=1 P(Aj), whenever A1, A2, . . . is a sequence of pairwisedisjoint sets in A (i.e. Aj ∩Ak = ∅ for all j 6= k).

This function P is called a probability measure (or just probability) on Ω. Twobasic examples are:

1. Discrete uniform space: Let Ω be a finite set and let N be the number ofelements in Ω. Let A be a σ–field of all possible subsets of Ω and let Pbe defined by:

∀a ∈ Ω, P(a) =1N

.

Then (Ω,A,P) is a probability space and for every A ⊂ Ω,

P(A) =#A

N,

where #A denotes the number of elements in A. Such probability measureis usually called the discrete uniform measure on Ω.

2. Let Ω be a subset of a real line (typically an interval, or a positive half-line).Let A be a Borel σ–algebra (i.e. the smallest σ–algebra containing allsubintervals of Ω). Let f be a piecewise continuous, nonnegative functionon R such that ∫ ∞

−∞f(x)dx = 1.

If P is defined by

P(A) =∫

A

f(x)dx,

then (Ω,A,P) is a probability space, and the function f is called theprobability density function of P.

Note: Because these two examples are by far the most important for us, andin both there is a “canonical” choice of A, from now on the role of A will bediminished to a point that it will be usually ignored. This will free us of certaintechnical issues.

Theorem 3.1 (basic properties of probability)

(i) P(∅) = 0.

(ii) for any finite sequence of pairwise disjoint sets A1, . . . , An

P(n⋃

j=1

Aj) =n∑

j=1

P(Aj)

,

5

Page 6: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(iii) for any set A ∈ Ω, P(A) = 1−P(Ac).

(iv) for any sets A,B ∈ A such that A ⊂ B we have P(A) ≤ P (B),

(v) for any sets A,B ∈ A, P(A ∪B) = P(A) + P(B)−P(A ∩B).

Proof: To prove (i) set Aj = ∅, j = 1, 2, . . . . Then Aj ’s are pairwise disjoint andtheir union is the empty set. Hence, by the third axiom, we have

P(∅) =∞∑

j=1

P(∅),

which means that we must have P(∅) = 0. Once we know (i), (ii) follows byextending the finite sequence A1, . . . , An to the infinite one by setting Aj = ∅, forj = n+1, n+2, . . . . Then

⋃∞j=1 Aj =

⋃nj=1 Aj and

∑∞j=1 Aj =

∑nj=1 Aj and (ii)

follows from the third axiom. The remaining properties are direct consequencesof axioms and (ii). For example to see (iii) notice that A, Ac are disjoint andtheir union is Ω. Hence

1 = P(Ω) = P(A ∪Ac) = P(A) + P(Ac),

which gives (iii). Similarly, for (iv) write B = A ∪ (Ac ∩B) with A and Ac ∩Bdisjoint. Thus,

P(B) = P(A ∪ (Ac ∪B)) = P(A) + P(Ac ∩B) ≥ P(A),

since by the first axiom P(Ac ∩ B) ≥ 0. A proof of the last part is similar andis omitted. ¤

The following is a useful observation.Disjointification: Let A1, A2, . . . , be a sequence of sets. Define their disjoin-tification B1, B2, . . . as follows:

B1 = A1,

Bk = Ac1 ∩Ac

2 ∩ · · · ∩Ack−1 ∩Ak, for k ≥ 2.

Then, the sets B1, B2, . . . , have the following two properties:

(i) P(⋃∞

j=1 Aj) = P(⋃∞

j=1 Bj), and

(ii) B1, B2, . . . , are pairwise disjoint.

4 Conditional Probability

Let (Ω,A,P) be a probability space and let A,B ∈ A, P(B) > 0. Then theconditional probability of A given B (denoted by P(A|B)) is defined by

P(A|B) =P(A ∩B)

P(B).

6

Page 7: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Note: The above formula is also frequently used in another form, namely, to

compute the probability of the intersection of two sets:

P(A ∩B) = P(A|B)P(B).

We say that a (possibly finite) sequence of sets A1, A2, . . . is a (measurable)

partition of Ω if

(i) ∀j ≥ 1 Aj ∈ A,

(ii) ∀j ≥ 1 P(Aj) > 0, and

(iii)⋃∞

j=1 Aj = Ω.

For example, if A ∈ A is such that 0 < P(A) < 1, then A,Ac is a partition ofΩ.

Theorem 4.1 (the law of total probability) Let A1, A2, . . . , be a (possibly finite)partition of Ω. Then for every set A ∈ A we have

P(A) =∑

j≥1

P(A|Aj)P(Aj).

Proof: Let Bj = A ∩ Aj . Then Bj ’s are pairwise disjoint (since Aj ’s are) andtheir union is A (because the union of all Aj ’s is Ω). Therefore,

P(A) = P(⋃

j≥1

Bj) =∑

j≥1

P(Bj) =∑

j≥1

P(A ∩Aj) =∑

j≥1

P(A|Aj)P(Aj),

where the last equality follows from the definition of the conditional probability.¤

5 Independence

Two sets, A, B ∈ A are independent if

P(A ∩B) = P(A) ·P(B).

Two sets that are not independent are called dependent.

Note: This definition and the definition of the conditional probability implythat A,B are independent iff P(A|B) = P(A) (and thus also P (B|A) = P(B)).If one thinks of the conditional probability of A given B as the probability ofA once we have information contained in B, then the last equation simply saysthat the information contains in B is totally irrelevant as far as chances of Aoccurring are concerned.We also note the following: if A,B are independent then so are A,Bc (and thusalso Ac, Bc, and Ac, B). Indeed,

P(A) = P(A ∩B) + P(A ∩Bc) = P(A)P(B) + P(A ∩Bc),

7

Page 8: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

by independence. Hence,

P(A ∩Bc) = P(A)−P(A)P(B) = P(A)(1−P(B)) = P(A)P(Bc),

i.e. A and Bc are independent.For more than two sets the definition becomes as follows: sets A1, A2, . . . , An

are independent if for any choice of any number of them, the probability of theintersection is equal to the product of probabilities, that is, if

∀ 2 ≤ m ≤ n, ∀ 1 ≤ j1 < j2 < · · · < jm ≤ n,

P(Aj1 ∩Aj2 ∩ · · · ∩Ajm) = P(Aj1) ·P(Aj2) · · · · ·P(Ajm

).

Note: This means, in particular, that three sets A,B, C are independent if all

of the following hold:

P(A ∩B) = P(A)P(B), P(A ∩ C) = P(A)P(C),P(B ∩ C) = P(B)P(C), P(A ∩B ∩ C) = P(A)P(B)P(C).

It is not enough that just the last equality holds.

6 Random Variables

A random variable X is a real valued function whose domain is a probabilityspace, i.e.

X : (Ω,A,P) −→ R.

Note: In general we need more, namely that this function X is measurable

which means that inverse image of any interval in R belongs to A, i.e. X−1(I) ∈A for any interval I. Since practically all of the functions we will considerwill satisfy that, (for example a random variable defined on a discrete uniformprobability space is automatically measurable since in this case A contains allsubsets of Ω) it will simplify our discussion if we just ignore that requirement.In our study of random variables we are usually interested not in the random

variables themselves, but, rather, in their distributions.

Definition: Let X be a random variable on (Ω,A,P) then, its distributionfunction F is a function F : R −→ [0, 1] defined by

∀x ∈ R, F (x) = P(X ≤ x).

Note: Distribution function is frequently called the cumulative distribution

function (abbreviated as c.d.f.). Also, sometimes we write FX to emphasize thatF is the c.d.f. of X.

Theorem 6.1 (basic properties of the c.d.f.)

8

Page 9: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(i) F is increasing in the sense that if x1 ≤ x2 then F (x1) ≤ F (x2),

(ii) limx→∞ F (x) = 1,

(iii) limx→−∞ F (x) = 0,

(iv) F is right continuous. That is, for every x and for every decreasing se-quence (xn) such that limn→∞ xn = x we have limx→∞ F (xn) = F (x).

Note: F does not have to be continuous in general.

Two special classes of random variables are: discrete and continuous. Arandom variable that takes on countably (or finitely) many values, each witha positive probability, is called a discrete random variable. For such a randomvariable we define its probability mass function p as follows:

p(x) = P(X = x).

Thus, p(x) is equal to 0, unless x is one of the values taken on by X. Suppose,for simplicity that x1 < x2 < . . . are all the possible values of X and setpk = P(X = xk). Then we have the following relationship:

F (x) =∑

j: xj≤x

pj .

Thus the c.d.f. of a discrete random variable is a step function, with stepsoccurring at the points xk, k = 1, 2, . . . .

We say that a random variable X is continuous if there exists a function f :R −→ R such that

(i) ∀ x ∈ R, f(x) ≥ 0.

(ii) ∀ x < y we have

P(x < X ≤ y) =∫ y

x

f(t)dt.

In that case the function f is called a density of X. Note that the left hand

side above can be expressed in terms of the c.d.f. of X as F (y)− F (x). This isbecause

X ≤ y = (X ≤ y ∩ X ≤ x) ∪ (X ≤ y ∩ X ≤ xc)= X ≤ x ∪ (X ≤ y ∩ X > x) .

Since the sets X ≤ x and X ≤ y ∩ X > x are disjoint, for probabilitieswe get

P(X ≤ y) = P(X ≤ x) + P(x < X ≤ y),

which means thatP(x < X ≤ y) = F (y)− F (x).

9

Page 10: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

In particular, letting x → −∞ we obtain

∀y ∈ R, F (y) =∫ y

−∞f(t)dt.

This is similar to a formula expressing the c.d.f. of a discrete random variable interms of its probability mass function. In any case, the point is that if we knowthe density (in the continuous case) or the probability mass function (in thediscrete case) then we can determine the c.d.f. and vice versa, if we know thec.d.f. of a discrete (or continuous) random variable X, then we can determinethe probability mass function (or density) of X.

Examples of the most important random variables (note that all of them aredescribed through their c.d.f’s – or densities/probability mass functions):

(I) Discrete:

(i) discrete uniform: A random variable which takes on each of a finitelymany values x1, x2, . . . , xn with equal probability (necessarily 1/n)is called a discrete uniform random variable.

(ii) Bernoulli with parameter p, 0 < p < 1: A random variable X whichis equal to 1 with probability p and is equal to zero with probability1− p, i.e.

P(X = 0) = 1− p, P(X = 1) = p,

is called Bernoulli (with parameter p) random variable.

(iii) Binomial: Let an experiment resulting either with a “success” withprobability p or a “failure” (with probability 1 − p) be repeated in-dependently n times. A random variable X that counts the numberof successes in these n trials is called binomial with parameters n, p.Thus, for any k, 0 ≤ k ≤ n,

P(X = k) =(

n

k

)pk(1− p)n−k.

(iv) Geometric with parameter p, 0 < p < 1: Suppose that the experi-ment in (iii) is repeated independently until a success is obtained forthe first time (and then it is stopped). A random variable X thatcounts how many repetitions are needed until this happens is calleda geometric random variable (with parameter p). Thus,

P(X = k) = (1− p)k−1 · p, for k = 1, 2, . . . .

Note: sometimes geometric random variable is defined as the num-ber of failures before the first success. In that case the formula forthe probability mass function becomes:

P(X = k) = (1− p)k · p, for k = 0, 1, 2, . . . .

10

Page 11: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(v) Poisson with parameter λ, λ > 0: Let λ be a positive number. Arandom variable X whose probability mass function is given by:

P(X = k) = e−λ λk

k!, for k = 0, 1, 2, . . . .

is called a Poisson random variable with parameter λ.

(II) Continuous:(i) uniform on [a, b]: Let a < b be real numbers. A random variable X

whose density is given by:

f(x) =

1b−a if a ≤ x ≤ b

0 otherwise

is called a uniform random variable on the interval [a, b].(ii) exponential with parameter λ, λ > 0: let λ be a positive number. A

random variable X whose density is given by

f(x) =

λe−λx if x ≥ 00 otherwise

is called a exponential random variable with parameter λ.(iii) Gaussian (or normal random variable with parameters µ, σ: Let µ

be a real number and let σ be a positive number. Then a randomvariable X whose density is given by

f(x) =1√2πσ

exp− (x− µ)2

2σ2, for −∞ < x < ∞,

is called a normal (or a Gaussian) random variable with parametersµ and σ2.

7 Expectations of Random Variables

Let X be a random variable on a probability space (Ω,A,P). Then the expectedvalue EX is defined by

EX =

∑j≥1 xjP(X = xj), if X is discrete with values x1, x2, . . .

∫∞−∞ xf(x)dx, if X is continuous with density f(x);

provided that the sum ∑

j≥1

|xj |P(X = xj),

or, respectively, the integral∫ ∞

−∞|x|f(x)dx,

11

Page 12: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

is finite.

Example:

(i) if X is a Bernoulli random variable with parameter p, then

EX = 0 ·P(X = 0) + 1 ·P(X = 1) = 0 · (1− p) + 1 · p = p.

(ii) if X is exponential with parameter 1, then its expected value is∫ ∞

−∞xf(x) =

∫ ∞

0

xe−xdx =∫ ∞

0

e−xdx = 1,

where the next to last equality is justified by integration by parts.

Linearity of expectation: The expected value is linear, that is, if X and Yare two random variables and a, b are two constants then

E(aX + bY ) = aEX + bEY.

Let X be a random variable on (Ω,A,P) and let h be a function (say, piecewisecontinuous) h : R −→ R. Then the composition Y = h(X) : Ω −→ R is againa random variable. Thus, we can talk about its expected value. We have:

EY = Eh(X) =

∑j≥1 h(xj)P(X = xj), if X is discrete

∫∞−∞ h(x)f(x)dx, if X is continuous with density f(x);

provided that the corresponding sum or integral is finite.

Example:

(i) (linearity) if X is a binomial random variable with parameters n, p, thenX =

∑nj=1 Xi, where

Xi =

1, if there is a success on the ith trial,0, otherwise.

We usually write Xi = I(Xi = 1) and call it the indicator function (of theset a success on the ith trial). Hence,

EX = E(n∑

j=1

Xj) =n∑

j=1

EXj =n∑

j=1

p = n · p.

(ii) if X is uniform on [0, 1] and h(x) = x3, then

EY = EX3 =∫ ∞

−∞x3f(x)dx =

∫ 1

0

x3dx =14.

12

Page 13: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

A few functions of particular importance:

(i) h(x) = x2, or more generally h(x) = xp, or h(x) = |x|p, p > 0. Then EXp

and E|X|p are called the pth and the absolute pth moment, respectively.

(ii) if for a random variable X, h(x) = (x−EX)2 then

Eh(X) = E(X −EX)2,

is called the variance of X and is denoted by var(X), or D2(X). Bysquaring out the term under the expected value sign and using linearitywe see that var(X) = EX2 − (EX)2.

(iii) if h(x) = et·x then the function t → M(t) = EetX is called the momentgenerating function of X. Its domain is the set of all t for which thecorresponding expression is finite.

(iv) (outside of real valued functions but barely) for a real t consider h(x) =eitx where i is the imaginary unit. Then the function φ(t) defined by

φ(t) = EeitX ,

is the characteristic function of X. Note that if Y = aX + b where a, b areconstants then

φY (t) = Eeit(aX+b) = eitbEeitaX = eitbφX(at).

Note: The advantage of the characteristic function over the moment generat-ing function is that the former always exists and its domain is always the wholereal line R.

8 Independent Random Variables

Two random variables X and Y are called independent if for all x, y we have

P(X ≤ x ∩ Y ≤ y) = P(X ≤ x)P(Y ≤ y).

The left hand side is usually written as P(X ≤ x, Y ≤ y) and the functionFX,Y (x, y) : R×R −→ R is called the joint distribution function of X and Y .The equation becomes

FX,Y (x, y) = FX(x) · FY (y),

i.e. the joint distribution function is the product of individual c.d.f’s. If we thejoint probability mass function of two discrete random variables X,Y by

pX,Y (x, y) = P(X = x, Y = y),

13

Page 14: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

then if x1, x2 . . . and y1, y2 . . . are values taken by X and Y , respectively, thenwe have

p(xj , yk) = P(X = xj , Y = yk).

Expressing the latter in terms of the c.d.f.’s by using

X = xj , Y = yk = X ≤ xj ∩ X ≤ xj−1c ∩ Y ≤ yk ∩ Y ≤ yk−1c,

and using independence we obtain that

pX,Y (x, y) = pX(x)pY (y).

By a similar argument using the fact that relationship for the joint density isgiven by

fX,Y (x, y) =∂2FX,Y (x, y)

∂x∂y,

at every point x, y at which the joint c.d.f. is differentiable, we see that in thecontinuous case, if X and Y are independent then

fX,Y (x, y) = fX(x)fY (y).

As a consequence we obtain that whenever X, Y are independent then

E(X · Y ) = E(X) ·E(Y ),

whenever E|X ·Y | < ∞. What is more, if h : R×R −→ is a function such thath(x, y) = h1(x) · h2(y), and X,Y are independent then

Eh(X, Y ) = Eh1(X) ·Eh2(Y ),

provided E|h(X, Y )| < ∞. For discrete X, Y the argument goes like this:

Eh(X,Y ) =∑

j≥1,k≥1

h(xj , yk)P(X = xj , Y = yk)

=∑

j≥1,k≥1

h1(xj)h2(yk)P(X = xj)P(Y = yk)

=∑

j≥1

k≥1

h1(xj)P(X = xj)h2(yk)P(Y = yk)

=∑

j≥1

h1(xj)P(X = xj)

k≥1

h2(yk)P(Y = yk)

= EXEY

where the second equality is by the property of the function h and independence,while the third one is by rearranging the order of summation (that’s where theassumption E|h(X, Y )| < ∞ is needed). For continuous variables, the argumentis essentially the same - one needs to replace the summation by integration.

14

Page 15: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Two key examples of functions h with that property are functions related to themoment generating function or characteristic function of the sum of independentrandom variables.

h(x, y) = et(x+y) = etx+ty = etx · ety = h1(x) · h2(y),

orh(x, y) = eit(x+y) = eitx+ity = eitx · eity = h1(x) · h2(y).

Thus, we have the following statement:

Theorem 8.1 If X,Y are independent random variables then

(i) the moment generating function of a sum X + Y is the product of themoment generating functions of X and Y (for those t for all three exist)

(ii) the characteristic function φX+Y (t) of the sum is the product of the char-acteristic functions φX(t) of X and φY (t) of Y , i.e.

φX+Y (t) = φX(t) · φY (t), for all t ∈ R.

The concept of independence for more than two random variables (say, X1, X2, . . . , Xn)is defined by requiring that for all choices of x1, x2, . . . , xn the events

X1 ≤ x1, X2 ≤ x2, . . . , Xn ≤ xn,

are independent. We then have the same property: the joint p.d.f. (or density)is the product of individual p.d.f.’s (or densities) and thus, if

h : Rn −→ R

has the property that

h(x1, x2, . . . , xn) = h1(x1) · h2(x2) · · · · · hn(xn),

then for independent random variables X1, X2, . . . , Xn we have

Eh(X1, X2, . . . , Xn) = Eh1(X1) ·Eh2(X2) · · · · ·Ehn(Xn).

In particular, the characteristic function of the sum of independent random vari-ables is the product of their characteristic functions. That is, if X1, X2, . . . , Xn

are independent random variables then

φX1+X2+···+Xn(t) = φX1(t) · φX2(t) · · ·φXn(t).

15

Page 16: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

9 Inversion Formula

Theorem 9.1 (inversion formula) Let X be a random variable with distributionF and characteristic function φ. Then for all a < b such that P(X = a) =P(X = b) = 0, the following holds

F (b)− F (a) = limT→∞

12π

∫ T

−T

e−ita − e−itb

itφ(t)dt.

Proof: First, write Taylor expansion of the exponential function (with the re-mainder in the integral form):

eix =n∑

k=0

(ix)k

k!+

in+1

n!

∫ x

0

(x− s)neisds.

By integration by parts we have∫ x

0

(x− s)n−1eisds =xn

n+

i

n

∫ x

0

(x− s)neisds,

Hence ∫ x

0

(x− s)neisds =n

i

(∫ x

0

(x− s)n−1eisds− xn

n

),

and sincexn

n=

∫ x

0

(x− s)n−1ds,

combining those two expressions we obtain:∫ x

0

(x− s)neisds =n

i

∫ x

0

(x− s)n−1(eis − 1)ds.

Substituting this integral into our Taylor’s expansion for eix we obtain anotherexpansion:

eix =n∑

k=0

(ix)k

k!+

in

(n− 1)!

∫ x

0

(x− s)n−1(eis − 1)ds.

If x > 0 then the absolute value of the last term on the right is no more than(recall that |eis| = 1 for real s)

∣∣∣∣in

(n− 1)!

∣∣∣∣ ·∣∣∣∣∫ x

0

(x− s)n−1(eis − 1)ds

∣∣∣∣ ≤1

(n− 1)!

∫ x

0

(x− s)n−1|eis − 1|ds.

Since |eis − 1| ≤ |eis|+ |1| ≤ 2, this is further upper-bounded by

2(n− 1)!

∫ x

0

(x− s)n−1ds =2xn

n!.

16

Page 17: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Essentially the same computation can be repeated for x < 0 and we get∣∣∣∣

in

(n− 1)!

∫ x

0

(x− s)n−1(eis − 1)ds

∣∣∣∣ ≤2|x|nn!

.

Repeating the same argument for the remainder in the first expansion of eix weget ∣∣∣∣

in+1

n!

∫ x

0

(x− s)neisds

∣∣∣∣ ≤|x|n+1

(n + 1)!.

Hence, combining those two bounds we derive a useful bound on the expansionof the function eix (we will need it later !):

∣∣∣∣∣eix −

n∑

k=0

(ix)k

k!

∣∣∣∣∣ ≤ min |x|n+1

(n + 1)!,2|x|nn!

,

valid for all n ≥ 0. In particular, letting n = 0 we obtain

|eix − 1| ≤ min|x|, 2.We now return to the proof. For simplicity we will prove it for continuousrandom variables. So, suppose that X has density f . Thus its characteristicfunction is φ(t) =

∫∞−∞ eitxf(x)dx, and therefore the expression

12π

∫ T

−T

e−ita − e−itb

itφ(t)dt

becomes12π

∫ T

−T

e−ita − e−itb

it

(∫ ∞

−∞eitxf(x)dx

)dt.

Rewriting this as a double integral and multiplying out we get

12π

∫ T

−T

∫ ∞

−∞

eit(x−a) − eit(x−b)

itf(x)dxdt.

By our previous estimates∣∣∣∣eit(x−a) − eit(x−b)

it

∣∣∣∣ =

∣∣eit(x−b)(eit(x−a−(x−b)) − 1

)∣∣|it|

=

∣∣eit(b−a) − 1∣∣

|t| ≤ min|t(b− a)|, 2|t| ≤ |b− a|,

which is bounded independently of t and x. Therefore, the double integralcan be computed as iterated integrals in any order. Exchanging the order ofintegration we see that it is:

12π

∫ ∞

−∞

(∫ T

−T

eit(x−a) − eit(x−b)

itdt

)f(x)dx.

17

Page 18: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

For the inner integral use the fact that eiu = cos u + i sin u and linearity of theintegral to get

1i

∫ T

−T

cos(t(x− a))t

dt − 1i

∫ T

−T

cos(t(x− b))t

dt

+∫ T

−T

sin(t(x− a))t

dt−∫ T

−T

sin(t(x− b))t

dt.

Since the interval of integration is symmetric about 0 and the first two integrandsare odd functions (and thus their integrals are zero) while the last two are even,this expression is equal to

2∫ T

0

sin(t(x− a))t

dt− 2∫ T

0

sin(t(x− b))t

dt.

Let us denote for T ≥ 0

S(T ) =∫ T

0

sin x

xdx

and let sign(u) be a function which is +1 if u > 0, is −1 if u < 0, and is 0 ifu = 0. By change of variables, for any v

∫ T

0

sin(tv)t

dt = sign(v) ·∫ T |v|

0

sin y

ydy = sign(v)S(T |v|).

Thus, substituting this into inner integral gives the following value for the doubleintegral

∫ ∞

−∞

(sign(x− a)

πS(T |x− a|)− sign(x− b)

πS(T |x− b|)

)f(x)dx.

It remains to take the limit as T →∞. It is known from calculus that

limT→∞

∫ T

0

sin x

xdx =

π

2, that is lim

T→∞S(T ) =

π

2.

Therefore the function

sign(x− a)π

S(T |x− a|)− sign(x− b)π

S(T |x− b|)

converges to 0 if x < a or x > b and to 1 if a < x < b. (It also converges to 1/2if x = a or x = b, but this is irrelevant.) Thus,

limT→∞

∫ ∞

−∞

(sign(x− a)

πS(T |x− a|)− sign(x− b)

πS(T |x− b|)

)f(x)dx

=∫ b

a

f(x)dx = F (b)− F (a).

18

Page 19: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

This proves the inversion formula. ¤Note: The inversion formula, allows one to recover the distribution functionof a random variable from its characteristic function (in principle at least).But, even more importantly, as a consequence, we have the so-called uniquenesstheorem, namely if two random variables have the same characteristic functionthen they have to have the same distribution function as well. Since, obviously,two random variables with the same distribution function have to have thesame characteristic function, this is an “if and only if” statement. In otherwords, characteristic function uniquely determines the distribution of a randomvariable.

10 Convergence in Distribution

Definition. Let X be a random variable with c.d.f. F (x), and let X1, X2 . . . bea sequence of random variables with c.d.f’s F1(x), F2(x), . . . , respectively. Wesay that the sequence (Xn), n ≥ 1 converges in distribution to X if

Fn(x) −→ F (x), as n →∞,

for all x ∈ R at which F (x) is continuous. We write

Xn =⇒ X

to indicate the convergence in distribution.

Note:

(i) Since F is nondecreasing it can have at most countably many points atwhich it is not continuous. Furthermore, in the most interesting cases Fis, in fact, continuous.

(ii) The definition is really about the convergence of distribution functionsof random variables, rather than random variables themselves. In partic-ular, the random variables X1, X2, . . . need not be defined on the sameprobability space. For that reason we will often speak of convergence ofdistributions and write

Fn =⇒ F.

Example: Let X1, X2, . . . be i.i.d. (independent and identically distributed)random variables, each having an exponential distribution with parameter 1(and thus the common c.d.f. is F (x) = 1−e−x). Let Mm = maxX1, X2, . . . , Xn,let Fn be the c.d.f. of Mn and set bn = ln n. Then we have

P(Mn − bn ≤ x) = P(Mn ≤ bn + x) = Fn(bn + x).

Since Mn ≤ z = Xj ≤ z ∀1 ≤ j ≤ n by independence Fn(z) = Fn(z). Thus

Fn(bn + x) = Fn(lnn + x) = Fn(lnn + x) =(1− e− ln n−x

)n

=(

1− e−x

n

)n

−→ e−e−x

.

19

Page 20: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Sincelim

x→−∞e−e−x

= 0, limx→∞

e−e−x

= 1,

and this function is continuous and nondecreasing, it is a c.d.f. (called a maximalvalue distribution). Thus,

Mn − ln n =⇒ Y,

where the c.d.f. of Y is the extreme value distribution.

The following observation is very useful.

Lemma 10.1 Let F, F1, F2, . . . be distribution functions such that Fn =⇒ F .Then there exist Y, Y1, Y2 . . . defined on the common probability space (Ω,A,P)and having distributions F, F1, F2, . . . respectively, and such that Yn(ω) → Y (ω)for all ω ∈ Ω.

Proof: We take as the common probability space the interval (0, 1) with Borelσ-algebra (i.e. σ-algebra generated by all intervals) and the Lebesgue measure(i.e. the unique measure P satisfying P((a, b)) = |b− a|). Define Yn and Y by

Yn(ω) = infx : ω ≤ Fn(x), Y (ω) = infx : ω ≤ F (x),(it helps to draw a picture; these are essentially inverse functions of Fn’s and F ,except that F and Fn’s need not be strictly increasing and thus may not havethe ”real” inverses). Then we have ω ≤ Fn(x) iff Yn(ω) ≤ x with the similarstatement for Y . Hence

P(ω : Yn(ω) ≤ x) = P(ω : ω ≤ Fn(x)) = Fn(x),

where the last equality holds by the definition of our (Ω,A,P). Thus, Yn hasFn as its c.d.f. and the same argument shows that Y has c.d.f. F . It remains toshow that Yn(ω) → Y (ω) for all ω ∈ (0, 1) (again, if one thinks of Yn as inversesof Fn’s then the fact that Fn converge implies the the inverses converge, too).For the general case there is a bit more to say: Let 0 < ω < 1 and let ε > 0.Pick an x such that

Y (ω)− ε < x < Y (ω) and such that F is continuous at x,

(since F is nondecreasing it has at most countably many points of discontinuity).Then F (x) < ω and since Fn(x) converges to F (x) it follows that for sufficientlylarge n’s we will have Fn(x) < ω as well; hence,

Y (ω)− ε < x < Yn(ω),

which implies that lim infn Yn(ω) ≥ Y (ω). To show that lim supn Yn(ω) ≤ Y (ω),pick any ω′ such that ω < ω′ and for a given ε choose a y such that

Y (ω′) < y < Y (ω′) + ε and such that F is continuous at y.

Then we haveω < ω′ ≤ F (Y (ω′)) ≤ F (y),

20

Page 21: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

and since Fn(y) converges to F (y) for sufficiently large n’s we also have ω ≤Fn(y) and thus Yn(ω) ≤ y < Y (ω′) + ε. Since ε is arbitrary, this implies thatlim supn Yn(ω) ≤ Y (ω′), whenever ω < ω′. Letting ω′ → ω we conclude thatlim supn Yn(ω) ≤ Y (ω), provided Y is continuous at ω. Since Y is nondecreasingit has at most countably many points of discontinuity. At any such point wecan redefine Yn(ω) = Y (ω) = 0. This does not change the distributions of Yor Yn’s (since they are changed at at most countably many points) and makesYn(ω) converge to Y (ω) for all ω ∈ (0, 1). ¤

Theorem 10.2 Let X, X1, X2, . . . be random variables with distribution func-tions F, F1, F2 . . . , respectively. The following two conditions are equivalent:

(i) Xn =⇒ X.

(ii) Ef(Xn) −→ Ef(X), for every bounded, continuous real valued functionf .

Proof of (i)=⇒ (ii). Suppose Fn =⇒ F and consider random variables Yn andY given by the above lemma. Let f : R −→ R be a continuous function suchthat |f(x)| ≤ K, for x ∈ R. Since Yn(ω) → Y (ω) for all ω ∈ (0, 1) and f iscontinuous, f(Yn(ω)) → f(Y (ω)) for all ω ∈ (0, 1) as well. That is,

∀ω ∈ (0, 1) ∀ε > 0 ∃k ∀n ≥ k, we have |f(Yn(ω))− f(Y (ω))| < ε.

Letting ε = 1m , m = 1, 2, . . . , and defining the sets

An,m = ω : |f(Yn(ω))− f(Y (ω))| ≥ 1m,

the above quantification can be restated as⋂

m≥1

k≥1

n≥k

Acn,m = Ω = (0, 1).

That means that∀m ≥ 1

k≥1

n≥k

Acn,m = Ω = (0, 1)

and since the sets ⋂

n≥k

Acn,m, k = 1, 2, . . .

form an increasing sequence whose union is the whole Ω we must have

limk→∞

P

n≥k

Acn,m

= 1.

This means that for any ε > 0 there exists a k0 such that for any k ≥ k0 wehave

P

n≥k

Acn,m

≥ 1− ε

21

Page 22: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

and thus for the complement

P

n≥k

ω : |f(Yn(ω))− f(Y (ω))| ≥ 1m ≤ ε.

Therefore,Ef(Yn) = Ef(Y ) + E (f(Yn)− f(Y ))

and its enough to show that the second term goes to 0. We have

|E (f(Yn)− f(Y ))| ≤ E |f(Yn)− f(Y )|

= E |f(Yn)− f(Y )| I ⋃

n≥k

|f(Yn(ω))− f(Y (ω))| ≥ 1m

+E |f(Yn)− f(Y )| I ⋂

n≥k

|f(Yn(ω))− f(Y (ω))| < 1m

(since |f(x)− f(y)| ≤ |f(x)|+ |f(y)| ≤ 2K)

≤ 2KP

n≥k

|f(Yn(ω))− f(Y (ω))| ≥ 1m +

1m

(provided n ≥ k0)

≤ 2Kε +1m

(since m and ε are arbitrarily large and small, respectively)−→ 0.

This proves that part. (A moments reflection on the order of choices of variousparameters might be helpful: to show that E |f(Yn)− f(Y )| goes to zero, wehave to show that for an arbitrary η > 0 there exists an n0 such that thisquantity is no more than η for all n ≥ n0. For our η pick m such that 1/m < η/2,then pick ε > 0 such that 2Kε < η/2 (K is given in advance.) For that ε thereexists a k0 with the properties described above. Now we may take n0 ≥ k0.)Proof of (ii)=⇒(i). Let x < y and define the function f(t) as follows

f(t) =

1 if t ≤ xy−ty−x if x ≤ t ≤ y

0 if t ≥ y

Then Fn(x) ≤ Ef(Xn) and Ef(X) ≤ F (y). Therefore, lim supn Fn(x) ≤ F (y),and letting y → x we get lim supn Fn(x) ≤ F (x), provided F is continuous atx. Similarly, for u < x F (u) ≤ lim infn Fn(x) and again, letting u → x we getF (x) ≤ lim infn Fn(x), provided F is continuous at x. Thus limn Fn(x) = F (x)for any x at which F is continuous. This completes the proof of the theorem.¤ We will need a few more results before the main theorem

22

Page 23: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Theorem 10.3 (Helly selection theorem) For every sequence of c.d.f ’s Fn, n =1, 2, . . . there exists a subsequence nk, k = 1, 2 . . . and a nondecreasing, right-continuous function F such that limk→∞ Fnk

(x) = F (x) at all points x at whichF is continuous.

Proof: Let r1, r2, . . . be an enumeration of rational numbers. Then Fn(rk) n ≥1, k ≥ 1 is a doubly indexed bounded sequence. We will use what is calleda diagonal method to find a subsequence nm such that Fnm

(rk) converges asm →∞ for all k ≥ 1. First, since Fn(r1) is bounded, there exists a subsequencen1

1, n12, . . . such that Fn1

m(r1) converges as m → ∞. Look at the values of F ’s

at r2 along that subsequence, i.e. at Fn1m

(r2), m ≥ 1. It is a bounded sequenceand thus there exists a subsequence n2

m, m ≥ 1 such that Fn2m

(r2) converges asm → ∞. Look at the values Fn2

m(r3) and continue the same argument. This

produces an array:

Fn11(r1) Fn1

2(r1) Fn1

3(r1) . . .

Fn21(r2) Fn2

2(r2) Fn2

3(r2) . . .

Fn31(r3) Fn3

2(r3) Fn3

3(r3) . . .

· · ·· · ·· · ·

This array has the property that the sequence converges in every row, andthat the sequence of indices in every row (except the first one, of course) is asubsequence of those in the previous one. Choose a diagonal (hence the name)nm = nm

m, m = 1, 2, . . . . Then the sequence Fnm(rk) converges as m → ∞for all k ≥ 1. Indeed, for a given k look at nm for m ≥ k. All of them are asubsequence of the indices in the kth row, and since Fnk

j(rk) converges as j →∞

it does so along any subsequence as well. Thus, for every rational number r,the limit limm→∞ Fnm(r) exists; let’s call it G(r). Now define

F (x) = infG(r) : x < r.It is obvious from the definition that F is nondecreasing. To show that it is right-continuous, pick any x and for an arbitrary ε > 0 find a rational r,r > x suchthat G(r) < F (x)+ ε. We have to show that F (y) → F (x) as y tends to x fromthe right. But, for any y such that x ≤ y < r we have F (y) ≤ G(r) < F (x) + ε.Since ε is arbitrary, and F (x) ≤ F (y) (F is nondecreasing) it follows thatF (y) → F (x) as y x. Finally, to show that Fnm(x) converges to F (x) atany continuity point x of F , for an ε > 0 first pick a y, y < x and such thatF (x) − ε < F (y) and then rational r and s such that y < r < x < s andG(s) < F (x) + ε. We have

F (x)− ε < G(r) ≤ G(s) < F (x) + ε, and Fn(r) ≤ Fn(x) ≤ Fn(s).

Hence,

lim supm

Fnm(x) ≤ lim supm

Fnm(s) = limm

Fnm(s) = G(s) < F (x) + ε,

23

Page 24: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

and

lim infm

Fnm(x) ≥ lim infm

Fnm(r) = limm

Fnm(r) = G(r) > F (x)− ε.

Since ε is arbitrary, it follows that limm Fnm(x) = F (x). This completes the

proof. ¤Note: Clearly, we have 0 ≤ F (x) ≤ 1, but F need not be a c.d.f. becausewe may fail to satisfy limx→∞ F (x) = 1, for example. (Consider for instanceFn(x) = 0 if x < n and 1 if x ≥ n; then F given by Helly’s theorem is identically0.) It would, therefore, be nice to have a condition that would guarantee thatthis F is, indeed a c.d.f. Hence the following concept:

Definition. A sequence of c.d.f’s Fn, n = 1, 2, . . . is tight if

∀ε > 0 ∃x, y ∀n ≥ 1 such that Fn(x) < ε, and Fn(y) > 1− ε.

Theorem 10.4 Let Fn be a sequence of c.d.f ’s. Then the following conditionsare equivalent:

(i) Fn, n = 1, 2 . . . is tight,

(ii) for every subsequence nk there exist a further subsequence nkm and a c.d.f.F such that Fnkm

=⇒ F .

Proof of (i)=⇒(ii). (that’s all that will be needed). Let Fn, n ≥ 1, be tight.Pick any subsequence and apply Helly’s theorem to that subsequence to geta nondecreasing, right-continuous F such that (a further) subsequence of Fn’sconverges to F at its continuity points. Call this final subsequence nj , j ≥ 1.All we need to do is to show that F is indeed a c.d.f., i.e. that we have

limx→−∞

F (x) = 0, and limx→∞

F (x) = 1.

To this end pick an ε > 0. By tightness, pick x0 and y0 such that Fn(x0) < ε,and Fn(y0) > 1 − ε for all n. By monotonicity of Fn’s, decreasing x0 andincreasing y0 if necessary, we can assume that both are continuity points of F .Thus, since Fnj (x0) → F (x0) and Fnj (y0) → F (y0) as j → ∞ we must haveF (x0) < ε and F (y0) > 1− ε, which, since ε was arbitrary, implies that

limx→∞

F (x) = 0, and limx→∞

F (x) = 1.

as required.lim

x→∞F (x) = 0, and lim

x→∞F (x) = 1.

Proof of (ii)=⇒(i). Suppose Fn, n ≥ 1, has the property that every subsequencehas the further subsequence which converges in distribution to a c.d.f. We willshow that Fn, n ≥ 1, has to be tight. Suppose it is not. Then there exists an

24

Page 25: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

ε > 0 such that for all a, b there exists an n such that Fn(b) − Fn(a) < 1 − ε.For that ε choose a subsequence nk such that Fnk

(k)− Fnk(−k) ≤ 1− ε. This

subsequence had a further subsequence nkm such that along that subsequenceFnkm

converges to a c.d.f F . Pick a0, b0 such that F (b0) − F (a0) > 1 − ε, andagain, by decreasing a0 and increasing b0 if necessary we can assume that bothare continuity points of F . Then Fnkm

(b0) → F (b0) and Fnkm(a0) → F (a0)

as m → ∞, and since for m large enough −km < a0 and km > b0 we haveFnkm

(−km) ≤ Fnkm(a0) and Fnkm

(km) ≥ Fnkm(b0). Therefore,

1− ε ≥ Fnkm(km)− Fnkm

(−km) ≥ Fnkm(b0)− Fnkm

(a0) −→ F (b0)− F (a0).

But that would mean that F (b0)− F (a0) ≤ 1− ε, which gives a contradiction,and thus proves that Fn, n ≥ 1, is tight. ¤ Finally,

Corollary 10.5 If Fn, n ≥ 1 is a tight sequence of c.d.f ’s with the propertythat each subsequence that converges in distribution converges to the same dis-tribution F , then

Fn =⇒ F.

Proof: Suppose it is not true that Fn =⇒ F . That means that there is acontinuity point of F x0 such that Fn(x0) does not converge to F (x0). But then,there exists an ε > 0 and a subsequence nk, k ≥ 1 such that |Fnk

(x0)−F (x0)| ≥ε. By tightness and previous result, this subsequence has a further subsequence,say, nkm along which Fnkm

converges, and by the assumption it must convergeto F . But this is impossible, since |Fnkm

(x0) − F (x0)| ≥ ε. That finishes theproof. ¤

11 Continuity Theorem

The following result is a major tool in establishing the convergence in distribu-tion

Theorem 11.1 (continuity theorem) Let X,X1, X2, . . . be random variableswith characteristic functions φ(t), φ1(t), φ2(t), . . . , respectively. The followingtwo conditions are equivalent

(i) Xn =⇒ X, as n →∞,

(ii) for all t ∈ R, φn(t) −→ φ(t) as n →∞.

Proof of (i)=⇒ (ii). This follows from the Theorem 10.2 applied to the functionsx → <(eitx), and x → =(eitx) (both continuous and bounded for every t).Proof of (i)=⇒ (ii). Let Fn be the c.d.f of Xn. We first show that Fn, n ≥ 1 istight. We have

1u

∫ u

−u

(1− φn(t))dt =1u

∫ u

−u

(1−EeitXn)dt

= E(

1u

∫ u

−u

(1− eitXn

)dt.

25

Page 26: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

To compute the inner integral, write eitXn = cos(tXn)+ i sin(tXn), use the factthat cosine is even and sine is odd, to get

1u

∫ u

−u

(1− eitXn)dt = 2− 21u

∫ u

0

cos(tXn)dt = 2− 2sin(uXn)

uXn.

Therefore,

E(

1u

∫ u

−u

(1− eitXn))

dt = 2E(1− sin(uXn)uXn

)

≥ 2E(1− sin(uXn)uXn

)I(|Xn| ≥ 2/u)

≥ 2E(1− 12)I(|Xn| ≥ 2/u) = P(|Xn| ≥ 2

u)

= P(Xn ≥ 2u

) + P(Xn ≤ − 2u

)

≥ 1− Fn(2u

) + Fn(− 2u

)

= 1−(

Fn(2u

)− Fn(− 2u

))

Now φ(0) = 1 and φ is continuous at zero, which follows from

|φ(h)− φ(0)| = |E(eitX − 1)| ≤ E|eitX − 1| ≤ Emin|hX|, 2= Emin|hX|, 2I(|X| ≤ 1/

√h)

+Emin|hX|, 2I(|X| > 1/√

h)

≤√

h + 2P(|X| > 1/√

h) −→ 0

as h → 0. Therefore, for any ε > 0 there exists a u > 0 such that

1u

∫ u

−u

(1− φ(t))dt < ε.

Since φn(t) → φ(t) for all t ∈ R, and the functions |1 − φn(t)| are bounded by2, it follows that

∫ u

−u

(1− φn(t))dt −→∫ u

−u

(1− φ(t))dt < ε,

and hence we will have

1u

∫ u

−u

(1− φn(t))dt < ε,

for n ≥ n0. Combining this with the earlier computation we see that, for n ≥ n0

we haveFn(

2u

)− Fn(− 2u

) ≥ 1− ε,

26

Page 27: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

for n ≥ n0. Decreasing u if necessary we may assume that the above inequalityholds for n less than n0 as well so that it holds for all n ≥ 1. And, since theabove inequality and the fact that 0 ≤ Fn(x) ≤ 1 imply that

Fn(2u

) ≥ 1− ε, and Fn(− 2u

) ≤ ε,

the tightness is proved. Now we can use the first part and the previous corollaryto complete the proof: pick any subsequence nk such that Fnk

converges in dis-tribution. Then by the first part φnk

(t) converge to φ(t) and by the uniquenesstheorem we must have that Fnk

=⇒ F . Hence, by the corollary Fn =⇒ F . ¤

12 Lindeberg’s Central Limit Theorem

Let for each n ≥ 1 Xn,1, Xn,2, . . . , Xn,rnbe independent random variables. Put

Sn = Xn,1 + Xn,2 + · · ·+ Xn,rn,

and suppose that

EXn,k = 0, var(Xn,k) = σ2n,k, and set s2

n =rn∑

k=1

σ2n,k.

Finally suppose that the following condition (usually referred to as Lindebergcondition) is satisfied:

∀ε > 0 limn→∞

rn∑

k=1

1s2

n

EXn,kI(|Xn,k| ≥ εsn) = 0.

Then we have:

Theorem 12.1 Under the above assumptions:

Sn

sn=⇒ N(0, 1),

where N(0, 1) denotes the standard normal (i.e. with mean zero and variance1) random variable.

Note:

(i) the assumption EXn,k = 0 is inessential since we can always replace Xn,k

by Xn,k −EXn,k.

(ii) the most common application is in the case when X1, X2, . . . is a onesequence of random variables and Sn is a sequence of partial sums, i.e.Xnk = Xk and rn = n.

27

Page 28: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Proof: The plan is to use the continuity theorem. Let’s begin with a simplelemma:

Lemma 12.2 Let z1, . . . , zm and w1, . . . , wm be complex numbers such that|zj | ≤ 1 and wj | ≤ 1 for j = 1, . . . ,m. Then

|z1 · z2 · · · zm − w1 · w2 . . . wm| ≤m∑

j=1

|zj − wj |.

Proof of Lemma 12.2: The proof is by induction over m. For m = 1 thestatement is obvious. Assuming it’s true for all 1 ≤ k ≤ m− 1 write

|z1 · z2 · · · zm − w1 · w2 . . . wm|= |z1 · z2 · · · zm − z1 · w2 . . . wm + z1 · w2 . . . wm − w1 · w2 . . . wm|≤ |z1(z2 · · · zm − w2 · · ·wm)|+ |(z1 − w1) · w2 · · ·wm|

≤ |z1|m∑

j=2

|zj − wj |+ |z1 − w1| · |w2| · · · |wm| ≤m∑

j=1

|zj − wj |,

completing aproof of the lemma. Coming back to the proof of the theorem, letφn,k and φ be the characteristic functions of Xn,k, Xn, respectively. By thecontinuity theorem we need to show that

∀ t ∈ R φn(t) −→ e−t2/2.

Since we assume that

s2n =

rn∑

k=1

σ2n,k = 1

it follows that

e−t2/2 = e−t22

Prnk=1 σ2

n,k =rn∏

k=1

e−t2σ2

n,k2 .

28

Page 29: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Therefore, by Lemma 12.2

|φn(t)− e−t22 | =

∣∣∣∣∣rn∏

k=1

φn,k(t)−rn∏

k=1

e−t2σ2

n,k2

∣∣∣∣∣ ≤rn∑

k=1

∣∣∣∣φn,k(t)− e−t2σ2

n,k2

∣∣∣∣

=rn∑

k=1

∣∣∣∣EeitXn,k − e−t2σ2

n,k2

∣∣∣∣

=rn∑

k=1

∣∣∣∣∣E(

1 + itXn,k −t2X2

n,k

2

)− e−

t2σ2n,k2 + eitXn,k − 1− itXn,k +

t2X2n,k

2

∣∣∣∣∣

≤rn∑

k=1

∣∣∣∣∣E(

1 + itXn,k −t2X2

n,k

2

)− e−

t2σ2n,k2

∣∣∣∣∣

+rn∑

k=1

∣∣∣∣∣E(

eitXn,k − 1− itXn,k +t2X2

n,k

2

)∣∣∣∣∣

=rn∑

k=1

∣∣∣∣∣1−t2σ2

n,k

2− e−

t2σ2n,k2

∣∣∣∣∣ +rn∑

k=1

E

∣∣∣∣∣eitXn,k − 1− itXn,k +

t2X2n,k

2

∣∣∣∣∣

We will show that each of the two sums goes to zero. For the first sum, we usethe fact that for every real (or complex) number z we have

|ez − 1− z| ≤∑

j≥2

|z|jj!

= |z|2∞∑

j=0

|z|j(j + 2)!

≤ |z|2∞∑

j=0

|z|jj!

= |z|2e|z|.

Applying the above to each of the terms and using the fact that σ2n,k ≤ s2

n = 1we get that the sum is bounded by

rn∑

k=1

t4σ4n,k

4e

t2σ2n,k2 ≤ t4

4e

t22

rn∑

k=1

σ4n,k.

Lindeberg condition (recall that s2n = 1) implies that

max1≤k≤rn

σ2n,k −→ 0, as n →∞.

Indeed, for every ε > 0 and every 1 ≤ k ≤ rn

σ2n,k = EX2

n,k = EX2n,kI(|Xn,k| < ε) + EX2

n,kI(|Xn,k| ≥ ε)

≤ ε +rn∑

k=1

EX2n,kI(|Xn,k| ≥ ε).

By the Lindeberg condition, the second term goes to zero, and since ε is arbi-trary, the whole expression vanishes as n →∞. Hence,

rn∑

k=1

σ4n,k ≤ max

1≤k≤rn

σ2n,k

rn∑

k=1

σ2n,k = max

1≤k≤rn

σ2n,k −→ 0, as n →∞,

29

Page 30: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

which implies that the first sum goes to zero for every real t. For the secondsum, we use the following estimate (see the proof of Theorem 9.1):

|eix − (1 + ix− 12x2)| ≤ min

|x|33!

, x2

.

Then

|eitXn,k − 1− itXn,k +t2X2

n,k

2| ≤ Emin

|tXn,k|33!

, t2X2n,k

≤ Emin |tXn,k|3

3!, t2X2

n,k

I(|Xn,k| < ε)

+Emin |tXn,k|3

3!, t2X2

n,k

I(|Xn,k| ≥ ε)

≤ E|t|3ε3!

X2n,kI(|Xn,k| < ε) + t2EX2

n,kI(|Xn,k| ≥ ε)

≤ |t|3ε3!

σ2n,k + t2EX2

n,kI(|Xn,k| ≥ ε),

and by adding the terms we see that the second sum is bounded by

ε|t|33

rn∑

k=1

σ2n,k + t2

rn∑

k=1

EX2n,kI(|Xn,k| ≥ ε),

which goes to zero because ε > 0 is arbitrary and because of Lindeberg’s condi-tion. The proof is completed. ¤ The following is

a useful corollary to the Lindeberg’s CLT. The condition below (referred to asLyapunov condition) assumes that the higher moments than the second exist,but is usually easier to check than Lindeberg condition.

Corollary 12.3 Let Xn,k, n ≥ 1, 1 ≤ k ≤ rn, be random variables such thatEXn,k = 0 and var(Xn,k) = σn,k, and set s2

n =∑rn

k=1 σ2n,k. Suppose further

that the following condition is satisfied for some p > 2:

limn→∞

1sp

n

rn∑

k=1

E|Xn,k|p = 0.

ThenSn

sn=⇒ N(0, 1).

Proof: We have

X2n,kI(|Xn,k| ≥ εsn) = 1 ·X2

n,kI

( |Xn,k|p−2

εp−2sp−2n

≥ 1)≤ |Xn,k|p

εp−2sp−2n

,

so that, summing up over k gives

1s2

n

rn∑

k=1

EX2n,kI(|Xn,k| ≥ ε) ≤ 1

εp−2

1sp

n

rn∑

k=1

E|Xn,k|p,

which shows that Lyapunov condition implies Lindeberg’s condition. ¤

30

Page 31: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

13 Random Permutations

Let π = (π1, π2, . . . , πn) be a permutation i.e. an order of 1, 2, . . . , n. Thereare n! different permutations of 1, 2, . . . , n. By a random permutation of1, 2, . . . , n we mean a permutation chosen according to the discrete uniformprobability measure on the set Πn of all permutations of 1, 2, . . . , n.

Let π = (π1, π2, . . . , πn) be a permutation of 1, 2, . . . , n. We define asequence of sequential ranks, sq(πi), i = 1, . . . , n as follows: sq(πi) = j if andonly if πi is the jth smallest element among π1, π2, . . . , πi. For example, ifπ = (3, 5, 2, 1, 4) is a permutation of 1, 2, 3, 4, 5, then the sequence of sequentialranks is sq(π) = (1, 2, 1, 1, 4).

Lemma 13.1 There is a one-to-one correspondence between permutations of1, 2, . . . , n and n-long feasible sequences of sequential ranks.

Proof. We first observe that every permutation has a unique sequence of sequen-tial ranks. Thus, it is enough to show that there is at most one permutationcorresponding to a sequence of sequential ranks. We will prove it by inductionon n. For n = 1 the statement is clear since there is only one sequence ofsequential ranks and only one permutation. Assuming the statement true for1 ≤ k ≤ n− 1 consider a sequence (s1, . . . , sn) of sequential ranks and supposethat π = (π1, . . . , πn) and σ = (σ1, . . . , σn) are two permutations correspondingto (s1, . . . , sn). Find a position of the letter n in both π and σ; specificallyassume πi = n = σj . We claim that necessarily i = j. If not, then supposethat i < j. But πi = n and i < j means that πj is not the largest amongπ1, . . . , πj and thus sq(πj) = sj < j. On the other hand, since σj = n we havesq(σj) = sj = j, a contradiction.

Having proved that the letter n is at the same position in both π and σ we cannow remove it. What is left are two permutations π and σ of 1, 2, . . . , n − 1with sequence of sequential ranks (s1, s2, . . . , si−1, si+1, . . . , sn). By inductivehypothesis we must have π = σ and inserting back n on its original place wesee that π = σ, thus completing the proof. ¤

Theorem 13.2 Let π = (π1, . . . , πn) be a random permutation of 1, 2, . . . , n.Let Yj = sq(πj), j = 1, . . . , n be a sequence of sequential ranks. Then Y1, . . . , Yn

are independent and Yj is a discrete uniform random variable on 1, . . . , j.Proof. Clearly, the values of Yj are in 1, . . . , j. To show that Yj is uniformpick a k such that 1 ≤ k ≤ j. Then, by the law of total probability

P(Yj = k) =n∑

m=k

P (Yj = k, πj = m)

=n∑

m=k

P (Yj = k | πj = m) ·P(πj = m)

31

Page 32: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

To compute the conditional probability notice that, given that πj = m, Yj =k means that exactly k− 1 numbers less than m are on the first j− 1 positions.There are

(m−1k−1

)choices of k−1 numbers less than m and there are

(j−1k−1

)·(k−1)!ways of arranging those numbers on the first (j − 1) positions. Next, we fill theremaining j − k positions among the first (j − 1) with numbers larger than m.This can be done in

(n−mj−k

) · (j − k)! ways. Finally, we arrange the remaining(n− j) numbers on the (n− j) unoccupied positions. There are (n− j)! ways todo that. Now using the fact that given πj = m, (π1, . . . , πj−1, πj+1, . . . , πn) isa random permutation of 1, . . . , n \ m, and putting the pieces together wesee that our conditional probability is

P (Yj = k | πj = m) =1

(n− 1)!

(m− 1k − 1

)·(

j − 1k − 1

)·(k−1)!

(n−m

j − k

)·(j−k)!(n−j)!

which, after using (j − 1k − 1

)=

(j − 1)!(k − 1)!(j − k)!

and cancellation, becomes

P (Yj = k | πj = m) =(j − 1)!(n− j)!

(n− 1)!

(m− 1k − 1

)·(

n−m

j − k

).

Hence,

P(Yj = k) =1n

n∑

m=k

(j − 1)!(n− j)!(n− 1)!

(m− 1k − 1

)·(

n−m

j − k

)

=(j − 1)!(n− j)!

n!

n∑

m=k

(m− 1k − 1

)·(

n−m

j − k

)

=1j· j!(n− j)!

n!

n∑

m=k

(m− 1k − 1

)·(

n−m

j − k

)

=1

j · (nj

)n∑

m=k

(m− 1k − 1

)·(

n−m

j − k

).

and the proof of this part will be complete once we realize that

n∑

m=k

(m− 1k − 1

)·(

n−m

j − k

)=

(n

j

).

But this is true since the left-hand side counts the number of j-element subsetsof 1, . . . , n by first picking an integer m between k and n and then choosingk − 1 integers less than m and j − k integers larger than m. Thus, we haveproved that Yj is uniform on 1, . . . , j for j = 1, . . . , n. Given that, a proofthat they are independent is easy. Let (s1, . . . , sn) be a feasible sequence of

32

Page 33: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

sequential ranks. Then, since there is only one permutation with that sequenceof sequential ranks, we must have

P

n⋂

j=1

Yj = sj =

1n!

.

On the other hand, since Yj is uniform on 1, . . . , j,

P(Yj = sj) =1j.

Hence,n∏

j=1

P(Yj = sj) =n∏

j=1

1j

=1n!

= P

n⋂

j=1

Yj = sj ,

which shows independence. ¤The last theorem has a number of useful consequences two of which are

given below. Let π = (π1, π2, . . . , πn) be a permutation. We say that πk is arecord if k = 1 or πk > πj for all 1 ≤ j < k. A pair (πi, πj) is an inversionif i < j and πi > πj . Clearly, a permutation of n letters can have up to nrecords and

(n2

)inversions. Let Rn(π) be the total number of records in π. For

example, if π = (3, 1, 2, 4, 6, 5) then R6(π) = 2, namely, 4, and 6 are the records.Similarly if Vn(π) denotes the total number of inversions then in this examplewe would have V6(π) = 3 since (3, 1), (3, 2), and (6, 5) are the only inversions.Suppose now that π is a random permutation. In that case Rn may be viewedas a random variable on the probability space of all permutations of n lettersequipped with the discrete uniform probability measure and the same appliesto Vn. What can we say about the distribution of these two random variables?.The following gives a rather complete answer.

Theorem 13.3 Let π be a random permutation of n distinct letters. Then, asn →∞, we have

(i) the total number of records, Rn satisfies

Rn − ln n√ln n

=⇒ N(0, 1).

(ii) the total number of inversions, Vn satisfies

Vn − n2/4n3/2/6

=⇒ N(0, 1).

Proof of (i): The argument rests on the following simple observation; πk is arecord if and only if sq(πk) = k. Hence,

Rn =n∑

k=1

I(sq(πk) = k)def=

n∑

k=1

Ik,

33

Page 34: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

where, by the previous result, Ik’s are independent and P(Ik) = 1/k. It followsthat

ERn =n∑

k=1

1k∼ ln n, and var(Rn) =

n∑

k=1

var(Ik) =n∑

k=1

1k

(1− 1

k

)∼ ln n,

and the conditions for the CLT hold since the Ik’s are bounded.Proof of (ii): The proof follows the same idea, namely we will link inversions tosequential ranks. Specifically, we have

Vn =n∑

k=1

Wk,

where Wk is the number of π1, π2, . . . , πk that are larger than πk. Clearly, Wk =k − sq(πk). Thus, Wk is uniform on 1, . . . , k − 1 and they are independent.Therefore,

EVn =n∑

k=1

EWk =n∑

k=1

1k

(k − 1)k2

=(n− 1)n

4.

Similarly,

var(Vn) =n∑

k=1

var(Wk) =n∑

k=1

(k − 1)(k + 1)12

∼ n3

36.

Finally,

EW 3k =

1k

k−1∑

j=1

j3 ≤ 1k

∫ k

0

x3dx = O(k3

),

which implies Lyapunov’s sufficient condition for the CLT. ¤

14 Trees

A tree is a hierarchical structure of nodes arranged in levels. The very top(we tend to picture trees upside down) level has only one node called a root.Immediate descendants of any node (linked to it by edges) are called the childrenof that node and a node directly above a given node is referred to as a parent ora father. A node without children is called a leaf or an external node; any othernode is an internal node. The depth, dk, of a node k of a node is the distance(counted in terms of levels) between the root and that node. The height hn of atree on n nodes is the maximal depth of a node, i.e. hn = maxdk : k is a node(clearly it is enough to consider leaves only).

We will be exclusively interested in binary trees, i.e. trees with the propertythat every node can have at most two children. In such a tree, there can beat most 2` nodes on any level ` and if this is the case for a particular levelwe say that that level is saturated. If all levels except possibly the last oneare saturated, the tree is called complete, and if the last level is saturated aswell the tree is called perfect. It will be convenient to consider a notion of an

34

Page 35: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 1: Extended complete binary tree on 6 nodes

extended binary tree. If T is a binary tree then its extension T is obtained byadding nodes so that every original node of T has exactly two children. We willuse the symbol Tn to indicate that a given extended tree T is an extension ofa tree on n nodes. Thus, the subscript n refers to the size of the original tree.Also, hn will denote the height of an extension of a tree T on n nodes. Figure1 depicts an extended, complete binary tree on 6 nodes.

We now make a few observations about binary trees that will be useful later:

Lemma 14.1 Let Tn be a binary tree on n nodes. Then, the height of itsextension satisfies

dlg(n + 1)e ≤ hn ≤ n,

where dxe is the smallest integer no less than x, and lg denotes the base 2logarithm.

Proof: The upper bound is trivial since a height of any tree on n nodes is atmost n− 1 and extending it increases the height by 1. For the lower bound wefirst note hn is at least as large as h∗n, the height of an extension of a complete

35

Page 36: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

tree on n nodes, so it is enough to show it for such a tree. Since all the levels0, 1, . . . , h∗n−2 are saturated (these are all but the last level of the original, nonextended tree), h∗n is the smallest integer satisfying

1 + 21 + · · ·+ 2h∗n−1 ≥ n,

i.e. 2h∗n − 1 ≥ n, which gives h∗n ≥ lg(n + 1), and since h∗n is an integer theinequality is also true with the ceiling. ¤

Lemma 14.2 For any tree Tn on n nodes Tn has exactly n + 1 nodes.

Proof: Let `n be the number of leaves of Tn. Then Tn has exactly n+ `n nodes.We will count all the children in Tn in two different ways. On one hand, everynode except the root is a child; thus there are n + `n − 1 leaves. On the otherhand, every internal node of T has exactly two children. Since there are exactlyn nodes like that, there must be 2n children. Thus

2n = n + `n − 1,

which gives the lemma. ¤

14.1 Decision Trees

A decision tree is a tree in which internal nodes represent queries and leavesrepresent outcomes.

Here is an example of a decision tree which represents an algorithm thatsorts a permutation (π1, π2, π3) of 1, 2, 3 (see Figure 2). It begins by inquiringif π1 ≤ π2 and proceeds to compare π2 with π3 or π1 with π3 according towhether the answer to the first query is “yes” or “no”. It then decides what theorder is, or continues to the next query. We note that this decision tree is anextended tree (namely, it is an extension of the tree of queries), and that leavesrepresents all possible permutations of 1, 2, 3. Thus there are 6 = 3! leaves.The depth of a given leaf is exactly equal to the number of queries needed to findout the permutation represented by that leaf. Thus, the height of this tree isthe the largest possible number of queries needed to find any permutation. Thisis usually called the worst case performance of an algorithm. In this example,3 is the largest possible number of comparisons needed to find out which ofthe 6 permutations is π1, π2, π3. Let us now look at a general situation. Allof these observations remain true: a decision tree for a particular algorithmwould be an extension of a tree of queries, and its height would be the worstcase performance of that algorithm. Now comes a key observation: no matterwhat algorithm is used, the decision tree will have n! leaves (each correspondingto a particular permutation). Thus, by the previous lemma the query tree (ofwhich our decision tree is extension) has n! − 1 nodes so its height is at leastdlg(n!−1+1)e ≥ lg(n!). This gives us the following lower bound on any sortingalgorithm based on comparisons.

Theorem 14.3 The worst case performance of any comparison based sortingalgorithm is at least n lg n−O(n) on an input of size n.

36

Page 37: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 2: Decision tree for an algorithm sorting three elements

Proof: At this point the proof is no more than a simple calculation. Since thefunction lg x is increasing we have

lg(n!) =n∑

k=1

lg n ≥∫ n−1

1

lg xdx = (n− 1) lg(n− 1)−O(n) = n lg n−O(n).

¤Thus we cannot hope to sort a permutation of 1, . . . , n any faster than in

n lg n comparisons in the worst case. However, the worst case behavior, althoughinformative does not tell the whole story. We will therefore consider the averagecase behavior. What is meant by that is the following: let a comparison basedsorting algorithm be given. Let Cn(π) be the number of comparisons requiredto sort a permutation π. Now, suppose that π is a permutation randomly chosenfrom the set Πn of all permutations of 1, 2 . . . , n. Viewed like that, Cn is arandom variable defined on the probability space Πn equipped with the discreteuniform probability measure P, and the average case behavior of our algorithmis simply the expected value ECn of Cn. We have

37

Page 38: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Theorem 14.4 The average case behavior of any comparison based sorting al-gorithm is at least n lg n−O(n).

Proof: Consider a decision tree of a given sorting algorithm. Label leaves bypermutations; then the depth of a given π is the number of comparisons requiredto sort π. Since the measure P is uniform on Πn we get

ECn =∑

π∈Πn

dπP(π) =1n!

π∈Πn

dπ,

so that the proof will be complete once we show that∑

π∈Πn

dπ ≥ n! lg n!.

But this is a general fact, that is true for any extended tree T k. As a matterof fact, for a tree T the quantity

∑d`, where the sum extends over all leaves of

T (that is exactly what we have on the left side) is referred to as the externalpath length and is usually denoted by X(T ). We claim

Lemma 14.5 For any tree Tk on k nodes we have

X(T k) ≥ (k + 1) lg(k + 1).

Proof of Lemma 14.5. We first note that X(T k) ≥ X(T ∗k ), where T ∗k is a anextension of a complete tree on k nodes. This is because if Tk is not completethen there must be a level with index smaller than hk that is not saturated.Moving now a node from the bottom level hk to that level will decrease theexternal path length in the extended tree. This process can be continued (eachtime decreasing the external path length) until all the levels, except possiblythe last one, are saturated.

It remains to find X(T ∗k ). Let h∗k be the height of T ∗k and Lk be the numberof leaves on the h∗kth level. The leaves are on the last two levels and there arek of them altogether. Thus,

X(T ∗k ) = h∗kLk + (h∗k − 1)(k + 1− Lk) = (h∗k − 1)(k = 1) + Lk.

But Lk is twice the number of internal nodes on the (h∗k − 1)th level and sinceall the earlier levels are saturated, it is

Lk = 2

k −

h∗k−2∑

j=0

2j

= 2

(k − (2h∗k−1 − 1)

)= 2(k + 1)− 2h∗k .

We now use h∗k = dlg(k + 1)e to get

X(T ∗k ) = (dlg(k + 1)e − 1) (k + 1) + 2(k + 1)− 2dlg(k+1)e

= (k + 1) (dlg(k + 1)e+ 1)− 2dlg(k+1)e

= (k + 1)(lg(k + 1) + 1 + x)− 2lg(k+1)+x

= (k + 1) lg(k + 1) + (k + 1)(1 + x− 2x),

38

Page 39: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

where we have set x = lg(k + 1) − dlg(k + 1)e. It is now enough to check thatf(x) = 1 = x − 2x is nonnegative on [0, 1] which is true since f(0) = f(1) = 0and f is concave on [0, 1]. ¤

15 Insertion Sort

Insertion sort algorithm is the simplest and most direct sorting algorithm. Theoutput will be given as a list, A[1..n]. We begin with an empty list and at the kthstep we will have an already sorted list A[1..k−1] and we will insert the next key,πk into its the proper position in that list, creating an ordered list A[1..k]. Ateach stage we will have an insertion strategy indicating successive comparisonswe are going to make to insert a key. These are conveniently represented byinsertion trees; for example a tree in Figure 3 represents a strategy to insert π7

that consists on first comparing it with A[4] and then, depending on whether π7

is smaller than A[4] or not, comparing it with A[3] or A[6], and continuing untila proper position is found. In principle, one can follow very different strategiesto insert different keys; in practice one does the same type of insertion at everystage. We will insist, however that strategies to insert keys are deterministic.Let Ti be an insertion tree for the ith key and let hi be the height of Ti. LetXi be the number of comparisons required at the ith stage. Then, the totalnumber of comparisons, Cn is simply the sum of Xi’s,

Cn =n∑

k=1

Xk,

and by the properties of random permutations X1, . . . , Xn are independent. Thefollowing theorem is a fairly general sufficient condition for the CLT

Theorem 15.1 With the above notation, suppose that max1≤k≤n

hk = o(√

var(Cn)).

Then, the central limit theorem holds:

Cn −ECn√var(Cn)

=⇒ N(0, 1).

Note: Our condition max1≤k≤n

hk = o(√

var(Cn)) is sometimes phrased as hn =

o(√

var(Cn)) under the assumption that the heights hk are nondecreasing (whichreasonable strategies would satisfy).Proof: We will check Lyapunov’s condition for the centered random variablesXk = Xk − EXk. Since the number of comparisons to insert a given key isat most the height of a corresponding insertion tree, we have Xk ≤ hk; hence|Xk| ≤ 2hk. Let p > 2 be any number. Then,

E|Xk|p = E|Xk|p−2X2

k ≤ (2hk)p−2EX2

k.

39

Page 40: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 3: Insertion tree for A[7]

Summing up we obtain

n∑

k=1

E|Xk|p ≤ 2p−2

(max

1≤k≤nhp−2

k

) n∑

k=1

EX2

k = 2p−2

(max

1≤k≤nhp−2

k

)var(Cn),

in view of our assumption, Lyapunov’s condition is evident. ¤We will now see how it works on two examples.

15.1 Linear Insertion Sort

Linear sort corresponds to a very naive strategy, namely at the kth stage westart comparing πk with A[k − 1], then (if necessary) with A[k − 2] and so onuntil we encounter an entry smaller than πk or exhaust the list (perhaps theeasiest way to avoid an innesential nuisance caused by exhausting the list is tostart not with an empty list but with a list consisting of A[0] = 0 before thefirst insertion). The corresponding insertion tree is depicted in Figure 4.

40

Page 41: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 4: Insertion tree for linear sort

The number of comparisons needed is the number of elements in the listA[1..k−1] that are larger than πk, plus 1 (to encounter an element smaller thanπk). In other words,

Xk = 1 + (k − sq(πk)) = 1 + Wk,

where Wk is uniform on 0, . . . , k − 1 as discussed in Theorem 13.3. But thismeans that Xk is uniform on 1, . . . , k. Hence, by exactly the same compu-tations as in Theorem 13.3 we obtain that ECn ∼ n2/4 and var(Cn) ∼ n3/36.Since the height of the kth insertion tree satisfies hk ≤ k ≤ n = o(n3/2) theCLT holds:

Theorem 15.2 The number of comparisons Cn required to sort a random per-mutation of 1, . . . , n satisfies

Cn − n2/4n3/2/6

=⇒ N(0, 1).

41

Page 42: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

This is clearly worse performance than the lower bound we know. Quitelikely, it is due to a very poor inserting strategy; the fact that at the kth stageA[1..k − 1] was sorted was not fully taken advantage of. We will now try toimprove on that.

15.2 Binary Insertion Sort

This algorithm as an insertion strategy uses a binary search tree. A binarysearch on an ordered list is a search that compares an element to be inserted tothe “middle” element in a (sorted) list and then, recursively, proceed to the leftor right “half” according to whether πk was smaller or larger than the elementit was compared to. Specifically, if a list of length m is to be searched thealgorithm compares with the dm/2eth element in that list. It follows that theinsertion tree for the kth element, k ≥ 2, is an extension of a complete binarytree on k − 1 nodes, and thus has height dlg(1 + (k − 1))e and it has k leaves.Figure 5 shows an example of a binary insertion tree. Furthermore, the leavesare only on the last two levels dlg ke − 1 and dlg ke. It will be convenient to usethe fact dlg ke = blg(k − 1)c+ 1. Thus, the number of comparisons to insert πk

is either blg(k − 1)c or one more than that, depending if πk falls into a leaf onthe next to last or the last level. By the properties of random permutations, πk

is equally likely to fall into any of the k leaves. It follows that the number ofcomparisons Xk satisfies

Xk = blg(k − 1)c+ Bk,

where Bk is a Bernoulli random variable with parameter pk and pk is the propor-tion of leaves on the last level. In particular, EBk = pk and var(Bk) = pk(1−pk).To compute pk let Lk denote the number of leaves on the last level, so thatpk = Lk/k. This number is twice the number of nodes on the level blg(k − 1)cin the nonextended complete tree on k − 1 nodes. Since all the previous levelsare saturated we count:

Lk = 2((k − 1)−

(20 + 21 + · · ·+ 2blg(k−1)c−1

))

= 2((k − 1)−

(2blg(k−1)c − 1

))

= 2(k − 2blg(k−1)c

).

Hence the expected value of the number of comparisons is

ECn =n∑

k=2

EXk =n∑

k=1

blg(k − 1)c+ pk

=n∑

k=2

blg(k − 1)c+

2k

(k − 2blg(k−1)c)∼

n−1∑

k=1

lg k + O(n)

∼ n lg n + O(n),

42

Page 43: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 5: Insertion tree for a binary insertion sort

by the same argument as used, for example, in 14.3. Asymptotic evaluation ofthe variance follows the same pattern. Let m = blg(n− 1)c so that n = 2m + r,where 1 ≤ r ≤ 2m Then, by independence of Xk’s we have

var(Cn) =n∑

k=2

var(Xk) =2m∑

k=2

var(Xk) +r∑

i=1

var(X2m+i)

=m−1∑

k=0

2k∑

i=1

var(X2k+i) +r∑

i=1

var(X2m+i)

=m−1∑

k=0

2k∑

i=1

p2k+i(1− p2k+i) +r∑

i=1

p2m+i(1− p2m+i)

We now recall thatp2k+i =

L2k+i

2k + i,

43

Page 44: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

andL2k+i = 2

(2k + i− 2blg(2k+i−1)c

)= 2

(2k + i− 2k

)= 2i.

Substituting yields

var(Cn) =m−1∑

k=0

2k∑

i=1

(p2k+i − p22k+i) +

r∑

i=1

(p2m+i − p22m+i)

=m−1∑

k=0

2k∑

i=1

2i

2k + i−

2k∑

i=1

(2i

2k + i

)2

+

r∑

i=1

2i

2m + i−

r∑

i=1

(2i

2m + i

)2

.

We now calculate

2k∑

i=1

i

2k + i=

2k∑

i=1

2k + i

2k + i− 2k

2k∑

i=1

12k + i

= 2k − 2k(H2k+1 −H2k),

where, Hj =∑j

i=1 1/i is the jth harmonic number. Similarly, letting H(2)j =∑j

i=1 1/i2 we obtain

2k∑

i=1

(i

2k + i

)2

=2k∑

i=1

(1− 2k

2k + i

)2

=2k∑

i=1

(1− 2

2k

2k + i+

22k

(2k + i)2

)

= 2k − 2k+1(H2k+1 −H2k) + 22k(H(2)

2k+1 −H(2)

2k ).

Hence, the term within the first sum for the formula for the variance of Cn is

2(2k − 2k(H2k+1 −H2k))− 4(2k − 2k+1(H2k+1 −H2k) + 22k(H(2)

2k+1 −H(2)

2k ))

= 3 · 2k+1(H2k+1 −H2k))− 2k+1 − 22(k+1)(H(2)

2k+1 −H(2)

2k )).

The same reasoning applies to the last term and yields

3 · 2m+1(H2m+r −H2m))− 2r − 22(m+1)(H(2)2m+r −H

(2)2m )).

As we will see in a minute, this last term contributes an interesting, albeit notuncommon for binary structures, feature. Using approximations

Hj = ln j + γ + O

(1j

)and H

(2)j =

π2

6− 1

j+ O

(1j2

),

44

Page 45: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

we obtain that

var(Cn) =m−1∑

k=0

3 · 2k+1 ln 2− 2k+1 − 22(k+1)

(2k+1 − 2k

22k+1

)+ O(1)

3 · 2m+1(ln(2m + r)−m ln 2)− 2r

−22(m+1)

(1

2m− 1

2m + r

)+ O(1)

= (6 ln 2− 4)m−1∑

k=0

2k + 6 · 2m(lg(2m + r)−m) ln 2− 2r − 4 · 2m

+44m

2m + r+ O(m)

= (6 ln 2− 6)2m − 2(r + 2m) + 6 · 2m(lg(2m + r)−m) ln 2

+44m

2m + r+ O(m).

Now recalling that n = 2m + r and letting fn = lg(2m + r)−m, 0 < fn ≤ 1, sothat

2m = 2lg(2m+r)−fn =n

2fn,

we see that the variance is

var(Cn) = (6 ln 2− 6)2m + 6 · 2mfn ln 2− 2(r + 2m)

+4n2

2m + r)22fn+ O(lnn)

=6(1 + fn) ln 2− 6

2fn· n− 2n + 22(1−fn) + O(lnn)

= Q(fn) · n + O(ln n),

where

Q(x) =6((1 + x) ln 2− 1)

2x− 2 + 22(1−x).

Note that Q(x) is nonconstant and that Q(0) = Q(1), and thus, var(Cn)/n hasan oscillatory behavior, depending on how far n is from a perfect power of 2(these oscillations are small at the level a bit higher than 0.15 . . . ). This doesnot change the fact that max1≤k≤n hk = o(

√var(Cn)), so that we have

Theorem 15.3 The total number of comparisons Cn required to sort a randompermutation of 1, . . . , n by a binary insertion sort satisfies

Cn − n lg n√Q(fn) · n =⇒ N(0, 1).

45

Page 46: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

16 Heap Sort

Heap sort is mathematically interesting (and challenging) sorting algorithm.Remarkably, it is designed so that its worst–case behavior is of optimal orderO(n log n). As is quite common in such situations, the average case analysis isquite complicated. As a matter of fact, the expected number of comparisonswas found, asymptotically, as late as 1993 by Schaeffer and Sedgewick. Theasymptotics of the variance is not known at this time, and neither is the limitingdistribution function. So, in this section we will focus on the worst–case analysis.

A heap (or more precisely a bottom–up heap is a complete binary tree whosenodes are represented by numbers and are arranged so that the following heapproperty is satisfied: for any subtree its root is the largest element of thatsubtree. Since heap is a complete binary tree, all its levels except possibly thelast one are saturated. By convention, the last level is filled from left to right.The reason for this is that we often identify a heap on n nodes with a list H[1..n]where H[1] is a root of a heap and for every k the entries H[2k] and H[2k + 1]are the children of H[k] (if they are in the list). Our convention about the lastlevel guarantees that there are no “holes” in the list H. Figure 6 depicts a heapcorresponding to the list

[10, 9, 7, 5, 8, 3, 6, 2, 4, 1].

Sorting via heap is a two stage process. Let us think of a permutation π as alist [π1, . . . , πn] and represent it as a tree with π2k and π2k+1 as children of πk.This tree obviously is not a heap in general since it may fail the heap property.The first step is to rearrange the nodes so that the result is a heap (we will callit a heapifying process), represented by a list H[1..n]. Once this is done we willtake advantage of the heap property: the largest element is at the root. Withthat given, sorting proceeds according to the following argument: exchange H[1]and H[n] (that’s where H[1] belongs) and consider the list H[1..n − 1]. Thislist is not a heap in general, since the root may not be the largest element, butonce we heapify it, we could repeat the same process over and over again, eachtime reducing the size of a heap by 1 until we reach size 1, at which point thearray will be sorted. It remains to find an efficient way to construct a heap andthen to heapify at every stage of the sorting process.

16.1 Construction Stage

Let π = [π1, . . . , πn] be given and let hn = dlg(n+1)e−1 = blg nc be the heightof the corresponding (complete) tree. The construction begins at the very end.Suppose for simplicity that n = 2k + 1 is odd and look at the subtree of size 3[πk, π2k, π2k+1] (if n were even this subtree would have size 2, but as will be seen,this would not affect the process). This subtree does not have to be a heap. Inorder to heapify it, we need to exchange πk with the larger of its two childrenπ2k and π2k+1, if necessary. That requires two comparisons (one if n were even)and at most one data move. This results in creating a small heap of height oneat the very end of our list. We refer to the process of moving up the larger of

46

Page 47: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 6: Heap representing [10, 9, 7, 5, 8, 3, 6, 2, 4, 1]

the two children as promotion. We now continue in the same fashion until weheapify the last two levels. At this point the nodes on the levels hn − 1 andhn form heaps of height 1. For example, consider a list [3, 8, 6, 2, 1, 7, 10, 5, 4, 9]which corresponds to a tree in Figure 7. As Figure 8 shows, once the nodesindicated by the arrows are exchanged, the result will be a partially heapifiedtree with heaps of heights 1 at the bottom. We now move to the level hn − 2.Consider any node at that level and a subtree of height at most 2 rooted at thatnode. As before, it does not have to be a heap, but we can heapify it by what isusually referred to as a sift down process. First, exchange the root of our subtreewith the larger of two children (two comparisons and at most one exchange).The result may not be a heap since a new subtree of height one rooted at thenode we just promoted is not guaranteed to be a heap, but if it is not we cancomplete heapifying process by exchanging with the larger of the two childrenonce again. We then complete heapifying at the (hn − 2)nd level and move tothe next one. In our example, this stage is depicted in Figure 9. In general, atevery stage we are dealing with a subtree whose all proper subtrees are already

47

Page 48: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 7: Tree corresponding to [3, 8, 6, 2, 1, 7, 10, 5, 4, 9]

heaps, and all we need to do is sift down the root to its proper position.Let us see now how many comparisons we need. As a matter of fact it is

more convenient to count the number of data moves and remember that thereare at most two comparisons for every move. In the worst case scenario at everystage of the sift down process a node may have to be moved all the way downto the bottom of the tree. The height of a tree is hn = blg nc. Thus, in thefirst step, a node at the root may have to be moved up to hn times, each of thetwo nodes on level one up to hn − 1 times, and so forth. Adding over all nodesthrough the levels 0, 1, . . . , hn − 1 we obtain an upper bound

1 · hn + 21 · (hn − 1) + ·+ 2hn−2 · 2 + 2hn−1 · 1 =hn∑

k=1

k · 2hn−k

≤ 2hn

∞∑

k=1

k · 2−k ≤ 2hn · 2 ≤ 21+lg n ≤ 2n.

Remembering that there are at most two comparisons for every data move,

48

Page 49: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 8: Heaps of heights 1

yields an upper bound of 4n comparisons for the construction stage. This iswell below our “benchmark” of n log n level. It remains to analyze the sortingprocess.

16.2 Sorting Stage

We consider a heap H[1..n] and will analyze the sorting phase. As we indicatedat the beginning we start by exchanging H[1] with H[n] and consider a n − 1long list

A1[1..n− 1] = [H[n],H[2], . . . ,H[n− 1]] .

We heapify this list by sifting down the element H[n] to its proper position,obtaining a heap H1[1..n−1]]. We now repeat the same procedure; we exchangeH1[1] with H1[n− 1], and then heapify the list

A2[1..n− 2] = [H1[n− 1], H1[2], . . . , H1[n− 2]] ,

49

Page 50: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 9: Heaps of heights 2

by sifting down H1[n − 1]. The process is continued until the length of anunsorted part is reduced to 1. The first iteration of the sift down process forour example is shown on figure 10

The analysis of the number of moves is very simple. In the worst casescenario at every stage a current root will have to be sifted down all the way tothe bottom. When k keys are left, k = n, n − 1, . . . , 2, the corresponding treehas height blg kc so that in the worst case scenario the number of moves is nomore than

n∑

k=2

blg kc ≤n∑

k=2

lg k ≤∫ n+1

2

lg xdx = n lg n + O(1).

By a naive argument, the number of comparisons is therefore at most 2n lg n +O(1). Actually one can do better by utilizing the following observation: at everystage all the proper subtrees of a tree are heaps, and thus, the nodes of any pathstarting at the level 1 are in a decreasing order. If a specific path was givenwe could perform a binary insertion in that path (note that that our naive sift

50

Page 51: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 10: Sift down process

down uses effectively a linear insertion, which we know has poor performance).When k keys are left the height of a tree is blg kc so that the insertion canbe done with dlg (blg kc)e comparisons (see a discussion in section on a binaryinsertion sort). It remains to identify a path along which a root will be moved.But that is easy: at every stage a node is moved toward the larger of its twochildren, and it takes only one comparison (namely, compare the children) toidentify which is larger. Thus, for example, in a heap depicted in figure 11 thepath of maximal sons is identified by first comparing the two nodes on level 1,then the two children of the larger node, 9, and so on). The path of maximalsons is: 9, 8, 1.

Thus, to sift down the root when k nodes are left requires hk = blg kccomparisons to identify the path (called the path of maximal sons) along whichthe root will be moved, and then dlg (blg kc)e comparisons to find its properposition along that path. Summing up over k yields:

n∑

k=2

blg kc+ dlg (blg kc)e+ O(1) = n lg n + O(n lg(lg n)).

51

Page 52: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Figure 11: Path of maximal sons

Hence we have:

Theorem 16.1 Heap sort will require no more than n lg n+O(n lg(lg n)) com-parisons to sort any permutation of n letters.

Note: One of the reasons for which the average case analysis poses a challengeis that the heapifying process at does not preserve randomness. Specifically, itis known that if π is a random permutation of n distinct keys, then after theconstruction phase the resulting heap is also random (that is to say, that eachof all heaps on n nodes is equally likely to be obtained as a result). However, itis also known, that after sifting down the root not every heap on n− 1 keys isobtained with the same probability.

17 Merge Sort

Merge sort is an algorithm that tries to utilize what is known as a divide andconquer idea: split the list to be sorted in two sublists, sort each of them and

52

Page 53: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

then merge the two (already sorted) lists together. There are two issues toconsider here, one is a merging method, the other is the relative lengths of twosublists to be merged. Fortunately, it turns out that if the two lists are ofessentially the same length (or more precisely they differ in length by at mostone) then the very simplest merging algorithm, called linear merge will producean optimal result. Let us begin, however, by general considerations. Supposethat L1(m) and L2(k) are two sorted lists, of lengths m and k, respectively, thatare to be merged. Let n = m+k and we assume without loss of generality, thatm ≤ k. Denote by Sm,k the worst–case scenario number of comparisons neededto merge these two lists. Consider a decision tree for a merging algorithm. Nomatter what merging method is used, the algorithm will have to be able to findeach of the possible configurations of the merged lists. There are

(nm

)different

ways to do that (since there are that many ways to place the elements of L1(m)on the n positions). Consequently a decision tree will have that many leaves,and thus the height no less than lg

(nm

). Suppose now that both lengths are

essentially the same, that is m = bn/2c and k = dn/2e. Then, using Stirling’sapproximation n! ∼ √

2πn(n/e)n we get(

n

bn/2c)

=n!

bn/2c!dn/2e ∼√

2πn(n/e)n

(√2πn/2(n/(2e))n/2

)2 =√

2√πn

2n,

and henceSbn/2c,dn/2,e ≥ n− 1

2lg n + O(1).

It is now time to describe how the linear merge merges two lists. It is con-venient to think of removing elements from the lists once their proper positionon the merged list is found. With that convention, merging works like this: wejust compare the smallest elements in the two lists, the smaller is removed fromits list and becomes the first element on the merged list. We then continue inthat manner until one of the lists is exhausted, at which point we just transferthe leftover from the other list. For example if L1 and L2 are given by

L1 = [3, 6, 8, 9], and L2 = [1, 2, 4, 5, 7],

linear merge would compare first 3 and 1 (removing 1), then 3 and 2 (removing2), then 3 and 4 (removing 3 from L1), then 6 and 4 (removing 4), etc. until,after comparing 8 and 7 (and removing the latter) L2 became empty. We thentransfer 8 and 9 from L1. How many comparisons does it take? We simplyobserve that as long as neither of the lists is empty, moving an element to thenew list requires one comparison. Thus letting Lm,k denote the leftover on theother list at the time that the first becomes empty, we have

Sm,k = n− Lm,k ≤ n− 1,

since clearly, 1 ≤ Lm,k ≤ k (recall that k ≥ m). This is asymptotically the sameas the lower bound for any merging if the two lists are balanced. Specifically,we have

n− 12

ln n + O(1) ≤ Sbn/2c,dn/2,e ≤ n− 1.

53

Page 54: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

This gives enough information to perform a competent worst case behavioranalysis. Letting Wn be the largest number of comparisons required to sort anypermutation of 1, 2, . . . , n by a linear merge sort. We then clearly have thefollowing recurrence:

Wn = Wbn/2c + Wdn/2e + n− 1.

To get a better feel, let us consider first the case when n is a perfect power of2, say n = 2j . The recurrence becomes

W2j = 2W2j−1 + 2j − 1,

and can be easily iterated to yield

W2j = 2W2j−1 + 2j − 1 = 22W2j−2 + 2(2j−1 − 1) + 2j − 1= 23W2j−3 + 22(2j−2 − 1) + 2(2j−1 − 1) + 2j − 1

= · · · = 2jW1 +j−1∑

i=0

2i(2j−i − 1) = j2j −j−1∑

i=0

2i = j2j − (2j − 1)

= n lg n− (n− 1),

which is of optimal order. What about a general n? The trick is to find a rightformulation of the last formula. It turns out that the following is true:

Theorem 17.1 The worst–case number of comparisons, Wn required to sortany permutation of n distinct keys by a linear merge sort satisfies

Wn = ndlg ne − (2dlg ne − 1).

Proof: The proof is by an easy induction on n considering separately case of neven or odd to remove the ceilings. For example, if n = 2` then

W2` = 2W` + 2`− 1 = 2(`dlg `e − (

2lg ` − 1))

+ 2`− 1

= 2`dlg 2`− 1e − 21+dlg `e + 2` + 1 = 2`dlg 2`e − 2l − 2dlg 2`e + 2` + 1= 2`dlg 2`e − 2dlg 2`e + 1,

and the same argument works for n = 2` + 1. ¤Note: Writing xn = dlg ne − lg n the formula for Wn can be rewritten as

Wn = n(xn + lg n)− (2xn+lg n − 1

)= n lg n + nf(xn) + 1,

where f(x) = x− 2x, for 0 ≤ x ≤ 1. Thus, the coefficient in front of the linearterm has a small periodic oscillation depending how far n is from a perfect powerof 2.

54

Page 55: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

18 Quick Sort

Quick sort is an extremely convenient appealing sorting algorithms. It is con-sidered to be one of the algorithms ”with greatest influence on the developmentof science and engineering in the 20th century” by Dongarra and Sullivan intheir introduction to Computing in Science & Engineering 2 (2000), 22–23. Itis based on a very simple (once you know it) but powerful observation. To sortan array, pick an arbitrary element from the list (called pivot) and compareevery other element of that list to that pivot; if it is smaller, put it to left ofthe pivot and if not put it to the right. This process creates two lists (one ofthem may be empty). While neither of them is sorted, the “left” array consistsof elements smaller that the pivot, and the “right” of elements larger than thepivot. Consequently, it suffices to sort each of the two sublists. This is nowdone recursively. The first question is how to pick a good pivot. If nothing isknow a priori about the order, then there is no reason why any particular choicewould be better than the other, so choosing pivot uniformly at random fromthe elements on the array might not be a bad choice. Since we will be sortinga random permutation of 1, 2, . . . , n the the distribution of the first elementis uniform over the range; thus we might as well pivot around the first entryof a given list. Below is a Maple code of a procedure (called firststep) thatillustrates just that together with an example:

firststep:=proc(A)local L,R,B,k;if nops(A)<=1 then RETURN(A)

elseL:=[];R:=[]; for k from 2 to nops(A)

doif A[k]<A[1] then L:=[op(L),A[k]]else R:=[op(R),A[k]]

fi;od;

RETURN(L,A[1],R);fi;

end;

firststep([6,4,7,9,3,5,2,8,10,1]);

[4, 3, 5, 2, 1], 6, [7, 9, 8, 10];

The procedure constructs the two lists L and R and returns them with thepivot A[1] in between. Of course, if we wanted to sort the array A rather thanasking for L and R we would have asked for the sorted L an R. That is easyenough to do. Here is the code for quick sort

qsort := proc (A)

55

Page 56: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

local L, R, B, k;if nops(A) <= 1 then RETURN(A)

else L := []; R := [];for k from 2 to nops(A)

doif A[k] < A[1] then L := [op(L), A[k]]

else R := [op(R), A[k]]end if

end do;B :=[op(qsort(L)), A[1], op(qsort(R))];RETURN(B)

end ifend proc;

and here is our example:

qsort([6,4,7,9,3,5,2,8,10,1]);

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

It is perhaps worth reiterating a nice property of random permutations: if Awas a random permutation, so was each of the L and R.

The analysis of the number of comparisons is facilitated by the followingobservation; let Cn be the number of comparisons needed to sort a randompermutation of length n. If the value of the pivot were k this number wouldsatisfy the following recurrence:

Cn = Ck−1 + Cn−k + n− 1,

with the initial condition C(0) = 0 and with the understanding that the se-quences (Cm) and (Cm) are independent and they have the same distribution.The reason is that if the pivot is k, then the left and right sublists have, respec-tively, k− 1 and n− k elements, and we need n− 1 comparisons to create theselists. As we said earlier, pivot’s value is random, and its distribution is uniformon 1, 2, . . . , n. Thus, letting Un be such a random variable, and writing C(k)instead of Ck we obtain the following recurrence for the distribution of C(n):

C(n) = C(Un − 1) + C(n− Un) + n− 1, n ≥ 1, C(0) = 0.

This recurrence is instrumental in the analysis of quick sort. We will first findthe expected value and the variance.

18.1 Expected Value and Variance

We have

Theorem 18.1 Let C(n) be number of comparisons required to sort a randompermutation of 1, 2, . . . , n by quick sort. Then, as n →∞, we have:

56

Page 57: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(i) EC(n) = 2n ln n + O(n),

(ii) var(C(n)) ∼(

7− 2π2

3

)n2.

Proof: We begin with part (i). Write

EC(n) = E(C(Un − 1) + C(n− Un)

)+ n− 1,

and then consider the expectation of the first term. We have

EC(Un − 1) =n∑

j=1

EC(Un − 1)I(Un = j)

=n∑

j=1

∑m

mP(C(Un − 1) = m, Un = j)

=n∑

j=1

∑m

mP(C(Un − 1) = m|Un = j)P(Un = j)

=1n

n∑

j=1

∑m

mP(C(Un − 1) = m|Un = j)

=1n

n∑

j=1

∑m

mP(C(j − 1) = m) =1n

n∑

j=1

EC(j − 1),

and, since Un− 1 has the same distribution as n−Un and Cm the same as Cm,we obtain exactly the same expression for EC(n−Un). This gives a recurrence

EC(n) =2n

n∑

j=1

EC(j − 1) + n− 1 =2n

n−1∑

j=0

EC(j) + n− 1.

To solve it we multiply both sides by n to get

nEC(n) = 2n−1∑

j=0

EC(j) + n(n− 1),

write a similar expression for (n− 1)EC(n− 1)

(n− 1)EC(n− 1) = 2n−2∑

j=0

EC(j) + (n− 1)(n− 2),

and subtract side by side, to get:

nEC(n)− (n− 1)EC(n− 1) = 2EC(n− 1) + n(n− 1)− (n− 1)(n− 2).

57

Page 58: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Rearranging terms and dividing by n(n + 1) yields,

EC(n)n + 1

=EC(n− 1)

n+ 2

n− 1n(n + 1)

=EC(n− 2)

n− 1+ 2

n− 2(n− 1)n

+ 2n− 1

n(n + 1),

which can be further iterated, and since EC(0) = EC(1) = 0, gives

EC(n)n + 1

= 2n∑

j=2

j − 1j(j + 1)

= 2n∑

j=2

1

j + 1− 1

j(j + 1)

= 2 ln n + O(1),

which givesEC(n) = 2n ln n + O(n),

as claimed.Proof of (ii). The proof of the second part rests on a similar argument. Wefirst introduce the following concepts: for two integer valued random variables,X and Y we let

E(X|Y ) =∑

j

I(Y = j)E(X|Y = j),

whereE(X|Y = j) =

∑m

mP(X = m|Y = j).

The second notion is called the conditional expectation of X given Y = j whilethe first is called thee conditional expectation of X given Y . Both are verycommon in any undergraduate course in probability. Note that E(X|Y ) is adiscrete random variable taking value E(X|Y = j) on the set Y = j. For thatreason we have:

E (E(X|Y )) =∑

j

P(Y = j)E(X|Y = j)

=∑

j

P(Y = j)∑m

mP(X = m|Y = j)

=∑

j,m

mP(X = m|Y = j)P(Y = j) =∑

j,m

mP(X = m,Y = j)

= EX.

This is really nothing more than the law of total probability. We now can write:

var(X) = E (X −EX)2 = E (X −E(X|Y ) + E(X|Y )−EX)2 =

= E

(X −E(X|Y ))2 + (E(X|Y )−EX)2

+2(X −E(X|Y ))(E(X|Y )−EX)

= EE(X −E(X|Y ))2|Y ) + E(E(X|Y )−EX)2

+2E ((X −E(X|Y ))(E(X|Y )−EX)) .

58

Page 59: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Since EX = EE(X|Y ) the second term is simply the variance of E(X|Y ). Fur-thermore, the term inside the first expectation would be the variance of X ifthe conditional expectations E( · |Y ) were used rather than “usual” (or uncon-ditional) expectations. This is usually referred to as the conditional variancegiven Y and we will denote it by varY ( · ). Thus, the first term can be written asEvarY (X), where varY (X) = E((X −E(X|Y ))2|Y ). It remains to consider thelast term. We claim that it is 0. To see this, insert the conditional expectationgiven Y to obtain

EE((X −E(X|Y ))(E(X|Y )−EX)|Y ),

and observe that on each of the sets Y = j the second factor is constant.Thus, it can be pulled in front of the conditional (but not the unconditional)expectation. Hence the last expression is equal to

E(E(X|Y )−EX)E((X −E(X|Y ))|Y ).

But now the second factor is 0 since EX = E(E(X|Y )). Thus we obtain aformula

var(X) = EvarY (X) + var(E(X|Y )),

which, as a matter of fact is true for general random variables as well. We willuse it with X = C(Un − 1) + C(n − Un) and Y = Un. One more property ofvariance that will be used is that it is invariant under shifts, i.e. for any constanta var(X + a) = var(X). We now can write:

var(C(n)) = var(C(Un − 1) + C(n− Un) + n− 1)= var(C(Un − 1) + C(n− Un))= EvarUn(C(Un − 1) + C(n− Un))

+var(E(C(Un − 1) + C(n− Un)|Un))

Now, given Un, C(Un − 1) and C(n− Un) are independent so that

EvarUn(C(Un − 1) + C(n− Un)) = EvarUn(C(Un − 1)) + EvarUn(C(n− Un)),

and just as before for the conditional expectations, we get

EvarUn(C(Un − 1)) =n∑

j=1

EvarUn(C(Un − 1)I(Un = j))

=n∑

j=1

EvarUn=j(C(Un − 1))P(Un = j)

=1n

n∑

j=1

EvarUn=j(C(Un − 1)) =1n

n∑

j=1

var(C(j − 1)).

Once again, the same expression is valid for EvarUn(C(n−Un)), and letting forsimplicity vk = var(C(k)) we obtain a recurrence for vn

vn =2n

n−1∑

j=0

vj + var(E(C(Un − 1) + C(n− Un)|Un)),

59

Page 60: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

which is the same form of recurrence that we had for expected value, except thatthe term n−1 there is replaced by a more complicated expression var(E(C(Un−1)+C(n−Un)|Un)). As we examine how we went about solving that recurrencewe realize that most of the argument could be easily carried over to our presentsituation. In fact, if we consider a recurrence

vn =2n

n−1∑

j=0

vj + tn

for any sequence tn (usually called the toll function) and repeat exactly thesame steps as before, we get that

vn

n + 1=

vn−1

n+

tnn + 1

− n− 1n(n + 1)

tn−1.

Iterating this recurrence, rearranging the terms in the resulting sum, and mul-tiplying by n + 1 at the end, yields

vn = (n + 1)v0 + tn + 2(n + 1)n−1∑

j=2

tj(j + 1)(j + 2)

, (1)

where, in our case, v0 = 0. In order to proceed we need some information onthe values of the tolls tn. We know from the computations for expectations thatE(C(Un − 1)|Un) ∼ 2Un ln(Un). Substituting this into our expression for tn weget (we do have the exact expression for the expected value, so it can be checkedby quite lengthy but straightforward calculations, that the error terms are ofsmaller order; we will omit that in order to keep the analysis as unobstructedas possible)

var(E(C(Un − 1) + C(n− Un)) ∼ 4var(Un ln(Un) + (n− Un) ln(n− Un)).

Now we have

Un ln(Un) + (n− Un) ln(n− Un)= Un(ln(Un)− ln n) + (n− Un)(ln(n− Un)− ln n) + Un ln n + (n− Un) ln n

= Un ln(

Un

n

)+ (n− Un) ln

(n− Un

n

)+ n ln n

= n

(Un

nln

(Un

n

)+

(1− Un

n

)ln

(1− Un

n

)+ ln n

).

As n →∞ Un/n becomes effectively U – the uniform random variable on [0, 1]and since u ln u is bounded on (0, 1], the variance becomes

n2

(var(U ln U + (1− U) ln(1− U)) + O

(1n

))

= n2(E(U ln U + (1− U) ln(1− U))2

−(E(U lnU + (1− U) ln(1− U)))2)

+ O(n).

60

Page 61: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Now,

EU ln U =∫ 1

0

x ln xdx = −14,

and

E(U ln U + (1− U) ln(1− U))2 =56− π2

18giving

tn = 4n2

(56− π2

18− 1

4

)+ O(n) ∼

(73− 2π2

9

)n2.

This in view of (1) yields

vn ∼(

73− 2π2

9

)n2 + 2

(73− 2π2

9

)n2 =

(7− 2π2

3

)n2,

and thus completes the proof. ¤How about the limiting distribution? This turns out to be more difficult issue

than what we have encountered so far and requires new techniques. Since someof these would require a substantial mathematical background we will restrictsourselves to an informal argument (which, however can be made rigorous).

18.2 Limiting Distribution: Heuristics

As usually, the first step is to normalize; to avoid writing the factor√

7− 2π2/3we divide by n and let

C(n)∗ =C(n)− 2n ln n

n.

Applying our basic recurrence and performing a sequence of algebraic manipu-lations, pretty much along the same lines as before, we get

C(n)∗ =C(Un − 1) + C(n− Un) + n− 1− 2n ln n

n=

C(Un − 1)− 2(Un − 1) ln(Un − 1) + C(n− Un)− 2(n− Un) ln(n− Un)n

+2(Un − 1) ln(Un − 1) + 2(n− Un) ln(n− Un) + n− 1− 2n ln n

n

=C(Un − 1)− 2(Un − 1) ln(Un − 1)

Un − 1· Un − 1

n

+C(n− Un)− 2(n− Un) ln(n− Un)

n− Un· n− Un

n

+2Un − 1

nln

(Un − 1

n

)+ 2

n− Un

nln

(n− Un

n

)+

n− 1− ln n

n

= C(Un − 1)∗ · Un − 1n

+ C(n− Un)∗ · n− Un

n

+2Un − 1

nln

(Un − 1

n

)+ 2

n− Un

nln

(n− Un

n

)+

n− 1− ln n

n.

61

Page 62: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

It is now tempting to pass to the limit with n. Since Un/n =⇒ U , assuming thatC(n) did converge in distribution to C∗, it would seem that this limit wouldhave to satisfy:

C∗ d= UC∗ + (1− U)C∗

+ 2U ln U + 2(1− U) ln(1− U) + 1,

where ” d=” means equality in distribution. This is actually the case, althoughformal argument is much more involved then what the outlined heuristics sug-gests. The main reason for being cautious is that, unfortunately, convergencein distribution does not share some of the properties of other types of conver-gence that we take for granted. For example, the product of two sequences eachof which converges in distribution, does not have to converge in distribution.Nonetheless, as we mentioned earlier, the result itself is true. Hence,

Theorem 18.2 The limiting normalized distribution C∗ of the number of com-parisons that it takes quick sort to sort a random permutation of 1, . . . , n isthe unique distribution with finite second moment which satisfies the followingdistributional equation:

C∗ d= UC∗ + (1− U)C∗

+ 2U ln U + 2(1− U) ln(1− U) + 1,

where C∗

is an independent copy of C∗ and U is a uniform random variable on[0, 1], independent of both C∗ an C

∗.

62

Page 63: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

Exercises

1. Prove the following properties of probability (those that are not proved inthe lecture notes).

(i) P(∅) = 0.

(ii) for any finite sequence of pairwise disjoint sets A1, . . . , An P(⋃n

j=1 Aj) =∑nj=1 P(Aj),

(iii) for any set A ∈ Ω, P(A) = 1−P(Ac).

(iv) for any sets A,B ∈ A such that A ⊂ B we have P(A) ≤ P (B),

(v) for any sets A,B ∈ A, P(A ∪B) = P(A) + P(B)−P(A ∩B).

2. Use a concept of disjointification to show the subadditivity of the probability:for any (not necessarily pairwise disjoint) sets A1, A2, . . . , we have:

P(∞⋃

j=1

Aj) ≤∞∑

j=1

P(Aj).

3. Show that an exponential random variable with parameter λ has a memory-less property i.e. that for all s, t > 0 we have:

P(X > s + t|X > t) = P(X > s).

4. Let X be uniform random variable on [a, b] and let c satisfy a < c < b. Showthat the random variable X conditioned on the event X ≤ c is uniformlydistributed on [a, c]. (This amounts to finding P(X ≤ x|X ≤ c).)

5. Show by appropriate substitution that if X is a normal random variable withparameters µ, σ2 then the random variable Y defined by

Y =X − µ

σ

has a normal distribution with parameters 0, 1.6. Show that an geometric random variable with parameter p has a memorylessproperty i.e. that for all natural numbers k, m we have:

P(X = k + m|X ≥ m) = P(X = k).

7. Find the characteristic functions of the following random variables:

(i) uniform on the interval [0, 1].

(ii) Poisson with parameter λ.

63

Page 64: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(iii) binomial with parameters n, p (keep in mind that it is the sum of n inde-pendent Bernoulli random variables with parameter p).

(iv) normal with parameters 0, 1, and then, by change of variables normal withparameters µ, σ2.

8. Show that

(i) the variance of the sum of two independent random variables is the sumof their variances i.e., that var(X + Y ) = var(X) + var(Y ) if X, Y areindependent (just square out inside the integral sign).

(ii) Extend this to an arbitrary number of independent random variables, i.e.if X1, . . . , Xn are independent with finite variances then

var(X1 + · · ·+ Xn) = var(X1) + · · ·+ var(Xn).

(iii) Find the variance of the binomial distribution with parameters n, p (see acomment for exercise 7(iii)).

9. Let X and Y be Poisson random variables with parameters λ and µ, re-spectively. Find the characteristic function of the sum X + Y and argue bythe uniqueness of the characteristic function that the distribution of X + Y isPoisson with parameter λ + µ.

10. Let Xn be a discrete uniform random variable on [0, 1] taking on n values,i.e.

P(Xn =k

n) =

1n

, for k = 0, 1, . . . , n− 1.

(i) Find its characteristic function φn(t) (note that the resulting sum is thesum of a geometric progression so it has a closed form).

(ii) Find the limitφ(t) = lim

n→∞φn(t), for all t ∈ R,

and identify it as a characteristic function of a certain random variable.

11. Let Xn be a binomial random variable with parameters n, 1/2.

(i) Find its characteristic function φn(t).

(ii) Find the limitφ(t) = lim

n→∞φn(t), for all t ∈ R,

and identify it as a characteristic function of a certain random variable.

64

Page 65: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

12. Let Xn be a Bernoulli random variable with parameter pn, n ≥ 1 and Xa Bernoulli random variable with parameter p. Find a necessary and sufficientcondition for Xn =⇒ X.

13. Let yn, n ≥ 1 be a sequence of points, and let Fn, n ≥ 1 be a sequence ofdistributions given by

Fn(x) =

0 if x < yn,1 if x ≥ yn

Show that Fn converge in distribution iff yn → x and that in this case Fn =⇒ F ,where

F (x) =

0 if x < y,1 if x ≥ y

14. Let X, X1, X2, . . . , be a sequence of random variables such that Xn =⇒ X.Let c, c1, c2, . . . be a sequence of positive numbers such that limn→∞ cn = c andlet b, b1, b2, . . . be a sequence of any numbers such that limn→∞ bn = b. Showthat

Xn − bn

cn=⇒ X − b

c.

15. Let Xn, n ≥ 1 be exponential random variables with parameters λn, n ≥ 1.Show that the sequence of their c.d.f’s is tight if and only if there exists a c > 0such that λn ≥ c for all n ≥ 1.

16. Show that for arbitrary sequence of random variables Xn, n ≥ 1, thereexist constants an, n ≥ 1 such that

anXn =⇒ 0.

17. (classical central limit theorem). Let Xn, n ≥ 1 be i.i.d. random variablessuch that EXn = 0 and var(Xn) = 1, for all n ≥ 1. Let Sn = X1+X2+· · ·+Xn,n ≥ 1. Use continuity theorem (and properties of characteristic functions) toshow that

Sn√n

=⇒ N(0, 1),

where N(0, 1) denotes the normal random variable with parameters 0 and 1.

18. Let X1, X2, . . . , be a sequence of independent random variables such thatP(Xn = 1) = 1/n and P(Xn = 0) = 1− 1/n, and let

Sn =n∑

k=1

Xk.

Show that the expected value and the variance of Sn are both asymptoticallyln n, and show that

Sn − ln n√ln n

=⇒ N(0, 1).

65

Page 66: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

19. Let X1, X2, . . . , be a sequence of independent random variables such thatXk is discrete uniform on 0, 1, 2, . . . , k − 1, i.e.

P(Xk = j) =1k

, for j = 0, 1, . . . , k − 1.

Let Sn =∑n

k=1 Xk. Show that the expected value and the variance of Sn satisfy

ESn ≈ n2

4, var(Sn) ≈ n3

36,

and use Lindeberg’s CLT to show that

Sn − n2/4n3/2/6

=⇒ N(0, 1).

20. Let (π1, π2, . . . , πn) be a random permutation of n distinct numbers. Let1 ≤ i1 < i2 < · · · < ik ≤ n be fixed. Show that (πi1 , πi2 , . . . , πik

) is a randompermutation of k distinct numbers. (That’s extremely useful property of randompermutations.)

21. Design a comparison based algorithm that finds a median (the 3rd largestelement) in a permutation of 5 distinct numbers (a good one would make nomore than 6 comparisons). Assuming that the permutation is random find theexpected number of comparisons made by your algorithm.

22. Let Tk be an insertion tree for a kth element in any insertion sort algorithm.

(i) Prove that the average number of comparisons needed to insert the jthkey is

X(Tk)k

.

(ii) Prove that for a binary insertion sort each insertion tree is a completebinary tree and thus that the binary insertion sort minimizes the aver-age number of comparisons needed to sort a random permutation by aninsertion sort.

23. Find the smallest and the largest number of comparisons needed to sortany permutation of 1, . . . , n by a linear insertion sort, and find a permutationgiving these values. How many comparisons will a binary insertion sort need tosort each of these two permutations?

24. Carry out a step–by–step construction of a heap from the list

[1, 12, 8, 4, 7, 10, 9, 3, 13, 2, 6, 11, 5].

25. Consider a construction stage of heap sort. Find a permutation of 1, . . . , 10that gives the worst possible performance during that stage. How many com-parisons does it make?

66

Page 67: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

26. Consider the list[x1, x2, x3, 4, 5, 2, 3, 8, 7, 9]

which is a permutation of 1, . . . , 10.(i) Partially out a heap construction for that data (that is you should end up

having heaps on all levels except the 0th and the 1st).

(ii) Now, assuming that the above permutation was chosen at random findthe expected number of moves needed to complete the heap construction.

27. (merge sort) Consider the leftover random variable Lm,k discussed in sectionabout merge sort algorithm.

(i) Prove that for a natural number r

P(Lm,k ≥ r) =

(m+k−r

k

)+

(m+k−r

m

)(

nm

) ,

where m + k = n.

(ii) Prove that for any random variable X with values in the set of naturalnumbers, one has

EX =∞∑

r=1

P(X ≥ r),

and use it to show that

ELm,k =k

m + 1+

m

k + 1.

(iii) Let n = 2j and let Cn be the number of comparisons needed to sort arandom permutation of n distinct numbers by a merge sort (with splits inequal lengths at every stage). Show that

EC2j =j−1∑

i=1

2i

(2j−i

(1− 1

2j−i−1 + 1

)),

and conclude that in that case ECn ∼ n lg n + O(n).

28. The following list is a permutation of the set 1, . . . , 8.[x1, 4, 6, 8, x2, 2, 3, 1].

Assuming that this permutation was chosen at random find the expected numberof comparisons needed to sort this list by quick sort.

28. (FIND) Consider the following FIND algorithm that finds, say, the smallestelement in an unsorted list (and which together with quick sort was designedby Hoare): create two lists, L and R just as in quick sort. Since the smallestelement has to be in L continue recursively until the list is reduced to a singleelement or to an empty list. Let Cn be the number of comparisons needed tocarry out this search on a list of n distinct elements.

67

Page 68: Probabilistic Analysis of Sorting Algorithms Lecture …phitczen/lectnotes.pdflent monograph on the topic, namely, a text by Hosam M. Mahmoud Sorting: A Distribution Theory, Wiley

(i) Assuming that the original list is a random permutation, write down arecurrence for Cn.

(ii) Use that recurrence to show that the expected value of Cn satisfies

ECn = 2n− 2Hn ∼ 2n,

where Hn =∑n

i=1 1/i is the nth harmonic number.

(iii) By a similar argument, show that

var(Cn) ∼ n2

2.

(iv) Now consider the normalized random variable

C∗n =Cn − 2n

n.

Use the same heuristics as for quick sort to derive a distributional equationfor the limiting distribution C∗ of C∗n, as n →∞. (It is, in fact, the uniquedistribution having second moment that satisfies this equation.)

Note: This is NOT the most efficient way to find the smallest (or thelargest element on a list. A simple linear scan comparing with a currentminimum needs exactly n− 1 comparisons).

68