# Random Quicksort

Given as input a set ${\displaystyle S}$ of ${\displaystyle n}$ numbers, we want to sort the numbers in ${\displaystyle S}$ in increasing order. One of the most famous algorithm for this problem is the Quicksort algorithm.

• if ${\displaystyle |S|>1}$ do:
• pick an ${\displaystyle x\in S}$ as the pivot;
• partition ${\displaystyle S}$ into ${\displaystyle S_{1}}$, ${\displaystyle \{x\}}$, and ${\displaystyle S_{2}}$, where all numbers in ${\displaystyle S_{1}}$ are smaller than ${\displaystyle x}$ and all numbers in ${\displaystyle S_{2}}$ are larger than ${\displaystyle x}$;
• recursively sort ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$;

The time complexity of this sorting algorithm is measured by the number of comparisons.

For the deterministic quicksort algorithm, the pivot is picked from a fixed position (e.g. the first number in the array). The worst-case time complexity in terms of number of comparisons is ${\displaystyle \Theta (n^{2})}$.

We consider the following randomized version of the quicksort.

• if ${\displaystyle |S|>1}$ do:
• uniformly pick a random ${\displaystyle x\in S}$ as the pivot;
• partition ${\displaystyle S}$ into ${\displaystyle S_{1}}$, ${\displaystyle \{x\}}$, and ${\displaystyle S_{2}}$, where all numbers in ${\displaystyle S_{1}}$ are smaller than ${\displaystyle x}$ and all numbers in ${\displaystyle S_{2}}$ are larger than ${\displaystyle x}$;
• recursively sort ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$;

## Analysis of Random Quicksort

Our goal is to analyze the expected number of comparisons during an execution of RandQSort with an arbitrary input ${\displaystyle S}$. We achieve this by measuring the chance that each pair of elements are compared, and summing all of them up due to Linearity of Expectation.

Let ${\displaystyle a_{i}}$ denote the ${\displaystyle i}$th smallest element in ${\displaystyle S}$. Let ${\displaystyle X_{ij}\in \{0,1\}}$ be the random variable which indicates whether ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are compared during the execution of RandQSort. That is:

{\displaystyle {\begin{aligned}X_{ij}&={\begin{cases}1&a_{i}{\mbox{ and }}a_{j}{\mbox{ are compared}}\\0&{\mbox{otherwise}}\end{cases}}.\end{aligned}}}

Elements ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are compared only if one of them is chosen as pivot. After comparison they are separated (thus are never compared again). So we have the following observations:

Observation 1: Every pair of ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are compared at most once.

Therefore the sum of ${\displaystyle X_{ij}}$ for all pair ${\displaystyle \{i,j\}}$ gives the total number of comparisons. The expected number of comparisons is ${\displaystyle \mathbf {E} \left[\sum _{i=1}^{n}\sum _{j>i}X_{ij}\right]}$. Due to Linearity of Expectation, ${\displaystyle \mathbf {E} \left[\sum _{i=1}^{n}\sum _{j>i}X_{ij}\right]=\sum _{i=1}^{n}\sum _{j>i}\mathbf {E} \left[X_{ij}\right]}$. Our next step is to analyze ${\displaystyle \mathbf {E} \left[X_{ij}\right]}$ for each ${\displaystyle \{i,j\}}$.

By the definition of expectation and ${\displaystyle X_{ij}}$,

{\displaystyle {\begin{aligned}\mathbf {E} \left[X_{ij}\right]&=1\cdot \Pr[a_{i}{\mbox{ and }}a_{j}{\mbox{ are compared}}]+0\cdot \Pr[a_{i}{\mbox{ and }}a_{j}{\mbox{ are not compared}}]\\&=\Pr[a_{i}{\mbox{ and }}a_{j}{\mbox{ are compared}}].\end{aligned}}}

We are going to bound this probability.

Observation 2: ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are compared if and only if one of them is chosen as pivot when they are still in the same subset.

This is easy to verify: just check the algorithm. The next one is a bit complicated.

Observation 3: If ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are still in the same subset then all ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ are in the same subset.

We can verify this by induction. Initially, ${\displaystyle S}$ itself has the property described above; and partitioning any ${\displaystyle S}$ with the property into ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$ will preserve the property for both ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$. Therefore Claim 3 holds.

Combining Observation 2 and 3, we have:

Observation 4: ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$ are compared only if one of ${\displaystyle \{a_{i},a_{j}\}}$ is chosen from ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$.

And,

Observation 5: Every one of ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ is chosen equal-probably.

This is because the Random Quicksort chooses the pivot uniformly at random.

Observation 4 and 5 together imply:

{\displaystyle {\begin{aligned}\Pr[a_{i}{\mbox{ and }}a_{j}{\mbox{ are compared}}]&\leq {\frac {2}{j-i+1}}.\end{aligned}}}
 Remark: Perhaps you feel confused about the above argument. You may ask: "The algorithm chooses pivots for many times during the execution. Why in the above argument, it looks like the pivot is chosen only once?" Good question! Let's see what really happens by looking closely. For any pair ${\displaystyle a_{i}}$ and ${\displaystyle a_{j}}$, initially ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ are all in the same set ${\displaystyle S}$ (obviously!). During the execution of the algorithm, the set which containing ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ are shrinking (due to the pivoting), until one of ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ is chosen, and the set is partitioned into different subsets. We ask for the probability that the chosen one is among ${\displaystyle \{a_{i},a_{j}\}}$. So we really care about "the last" pivoting before ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ is split. Formally, let ${\displaystyle Y}$ be the random variable denoting the pivot element. We know that for each ${\displaystyle a_{k}\in \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$, ${\displaystyle Y=a_{k}}$ with the same probability, and ${\displaystyle Y\not \in \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$ with an unknown probability (remember that there might be other elements in the same subset with ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$). The probability we are looking for is actually ${\displaystyle \Pr[Y\in \{a_{i},a_{j}\}\mid Y\in \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}]}$, which is always ${\displaystyle {\frac {2}{j-i+1}}}$, provided that ${\displaystyle Y}$ is uniform over ${\displaystyle \{a_{i},a_{i+1},\ldots ,a_{j-1},a_{j}\}}$. The conditional probability rules out the irrelevant events in a probabilistic argument.

Summing all up:

{\displaystyle {\begin{aligned}\mathbf {E} \left[\sum _{i=1}^{n}\sum _{j>i}X_{ij}\right]&=\sum _{i=1}^{n}\sum _{j>i}\mathbf {E} \left[X_{ij}\right]\\&\leq \sum _{i=1}^{n}\sum _{j>i}{\frac {2}{j-i+1}}\\&=\sum _{i=1}^{n}\sum _{k=2}^{n-i+1}{\frac {2}{k}}&&({\mbox{Let }}k=j-i+1)\\&\leq \sum _{i=1}^{n}\sum _{k=1}^{n}{\frac {2}{k}}\\&=2n\sum _{k=1}^{n}{\frac {1}{k}}\\&=2nH(n).\end{aligned}}}

${\displaystyle H(n)}$ is the ${\displaystyle n}$th Harmonic number. It holds that

{\displaystyle {\begin{aligned}H(n)=\ln n+O(1)\end{aligned}}}.

Therefore, for an arbitrary input ${\displaystyle S}$ of ${\displaystyle n}$ numbers, the expected number of comparisons taken by RandQSort to sort ${\displaystyle S}$ is ${\displaystyle \mathrm {O} (n\log n)}$.

# Random Recurrence

We consider the selection problem: given as input a set ${\displaystyle S}$ of ${\displaystyle n}$ distinct numbers and an integer ${\displaystyle 1\leq k\leq n}$, find the ${\displaystyle k}$-th smallest number in ${\displaystyle S}$.

We know that this problem can be solved deterministically by the median-of-median algorithm or randomly by the LazySelect algorithm, both in linear time. In the same spirit of random QuickSort, we propose the following random QuickSelection algorithm, called the RandomQS.

 RandomQS(${\displaystyle S}$,${\displaystyle k}$) if ${\displaystyle |S|=1}$ return the only element in ${\displaystyle S}$; pick a uniform random pivot ${\displaystyle x\in S}$; construct ${\displaystyle S_{1}=\{y\in S\mid y\leq x\}}$ and ${\displaystyle S_{2}=\{y\in S\mid y>x\}}$; if ${\displaystyle |S_{1}|\geq k}$: return RandomQS${\displaystyle (S_{1},k)}$; if ${\displaystyle |S_{1}|: return RandomQS${\displaystyle (S_{2},k-|S_{1}|)}$;

It can be verified as an exercise problem that the expected number of comparisons of RandomQS is ${\displaystyle O(n)}$. Alternatively, we ask the following question:

• How many recursive calls to RandomQS are made in expectation in a running of the algorithm?

Let ${\displaystyle T(n)}$ denote the number of recursive calls in a running of RandomQS. Obviously ${\displaystyle T(n)}$ is a random variable and it satisfies the following recurrence:

${\displaystyle T(n)={\begin{cases}1+T(n-X(n))&n>1\\0&n=1\end{cases}}}$

where ${\displaystyle X(n)}$ is a random variable representing the number of elements in the input ${\displaystyle S}$ which are thrown away in the current call. Specifically, ${\displaystyle X(n)=|S_{2}|=n-|S_{1}|}$ when ${\displaystyle |S_{1}|\geq k}$, and ${\displaystyle X(n)=|S_{1}|}$ when ${\displaystyle |S_{1}|. We are looking for an upper bound on the expectation ${\displaystyle \mathbf {E} [T(n)]}$.

## The token game

The above recursion can be seen as the following token game:

• Initially we have ${\displaystyle n}$ tokens.
• In each round, we independently draw a random number ${\displaystyle 0\leq X(m)\leq m}$ which depends on the current number of tokens ${\displaystyle m}$ and throw away ${\displaystyle X(m)}$ tokens.
• The game terminates when there is no token left.

The number of rounds of the game is given by the random variable ${\displaystyle T(n)}$.

Needless to say, this procedure has some generality. It is actually pretty common in randomized algorithms. The followings are some examples:

Survival game for coins (with an application in Skip List)
Suppose we start with ${\displaystyle n}$ biased coins, each with probability ${\displaystyle p}$ being a HEAD. In each round, we independently flip every coins and remove those coins whose outcome is TAIL. We play the game until no coin left. The number of rounds is given by ${\displaystyle T(n)}$, satisfying the recursion
${\displaystyle T(n)={\begin{cases}1+T(n-X(n))&n>0\\0&n=0\end{cases}}}$
where ${\displaystyle X(n)}$ gives the number of TAILs, which follows the binomial distribution ${\displaystyle B(n,p)}$.
This quantity has an application in the analysis of the Skip List, a randomized data structure. A Skip List is a multi-level linked list, in which we start with a standard linked list of ${\displaystyle n}$ nodes, and each node in the list is lifted to the upper level independently with a fixed probability ${\displaystyle p}$. This lifting process is recursively applied until there is no node left. The number of levels of a Skip List is given by ${\displaystyle T(n)}$.
Coupon collector
Suppose a coupon collector collecting totally ${\displaystyle n}$ coupons by uniform independent trials. For each ${\displaystyle 0\leq k\leq n}$, let ${\displaystyle T(k)}$ be the number of trials to take when there are ${\displaystyle k}$ coupons remaining to collect. ${\displaystyle T(k)}$ satisfies the recursion
${\displaystyle T(k)={\begin{cases}1+T(k-X(k))&k>0\\0&k=0\end{cases}}}$

where ${\displaystyle X(k)}$ is the number of trials taken when there are exactly ${\displaystyle k}$ coupons remaining to collect, which we know satisfy that

${\displaystyle X(k)={\begin{cases}1&{\text{with probability }}{\frac {k}{n}},\\0&{\text{with probability }}1-{\frac {k}{n}}.\end{cases}}}$
Geometric distribution
Any geometric random variable can be described as a ${\displaystyle T(1)}$ defined as follows
${\displaystyle T(1)=1+T(1-X(1))}$ and ${\displaystyle T(0)=0}$
where ${\displaystyle X(1)\in \{0,1\}}$ is a Bernoulli random variable.

## Karp-Upfal-Wigderson bound

The following theorem was proved by Karp, Upfa, and Wigderson, and later was improved by Karp. Today it is known as the Karp-Upfal-Wigderson bound.

 Theorem (Karp-Upfal-Wigderson 1988) Consider the following recurrence {\displaystyle {\begin{aligned}T(n)&={\begin{cases}1+T(n-X(n))&n>c\\0&n=c\end{cases}}\end{aligned}}} where ${\displaystyle c}$ is a constant and each ${\displaystyle X(n)}$ is an independent drawn of a nonnegative integral random variable ${\displaystyle X_{n}}$ such that ${\displaystyle 0\leq X_{n}\leq n-c}$. If there exists a positive-valued non-degreasing function ${\displaystyle \,\mu (x)}$ satisfying that ${\displaystyle \mathbf {E} [X_{n}]\geq \mu (n)}$ for all ${\displaystyle n}$, then ${\displaystyle \mathbf {E} [T(n)]\leq \int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x.}$

The theorem gives an upper bound on the expected number of rounds of the token game if we can nicely lower bound the expectation of each ${\displaystyle X_{n}}$. The theorem itself is quite intuitive: for each ${\displaystyle k}$, ${\displaystyle X_{k}}$ gives the rate at which the number of tokens drops from ${\displaystyle k}$. At this rate it takes ${\displaystyle 1/X_{k}}$ rounds to cross from ${\displaystyle k}$ to ${\displaystyle k-1}$ tokens. From the point of view of an interval ${\displaystyle (x-\epsilon ,x]}$, we do not know which ${\displaystyle X_{k}}$ we are using, but for any ${\displaystyle k\geq x}$ the average rate is lower bounded by ${\displaystyle \mu (k)\geq \mu (x)}$. Taking an integration, it is not surprising that it takes at most ${\displaystyle \int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x}$ rounds on average to consume all but ${\displaystyle c}$ tokens. The only issue in this informal argument is that we do not have ${\displaystyle \mathbf {E} [1/X]=1/\mathbf {E} [X]}$, so this is not a rigorous proof. But with bit care we can still manage to prove the theorem rigorously.

Proof.
 We prove the theorem by applying an induction on ${\displaystyle n}$. The bound is trivially true when ${\displaystyle n=c}$. For ${\displaystyle n>c}$, assume the induction hypothesis for all smaller ${\displaystyle n}$. Denote that ${\displaystyle X=X(n)}$. Due to the recursion ${\displaystyle T(n)=1+T(n-X(n))}$, we have {\displaystyle {\begin{aligned}\mathbf {E} [T(n)]&=1+\mathbf {E} [T(n-X)]\\&=1+\mathbf {E} [T(n-X)\mid X=0]\Pr[X_{n}=0]+\mathbf {E} [T(n-X)\mid X>0]\Pr[X>0].\end{aligned}}} Denote that ${\displaystyle p=\Pr[X=0]}$. We have ${\displaystyle \Pr[X>0]=1-p}$ because ${\displaystyle X=X(n)}$ is nonnegative. Thus the above equation becomes {\displaystyle {\begin{aligned}\mathbf {E} [T(n)]&=1+p\mathbf {E} [T(n)]+(1-p)\mathbf {E} [T(n-X)\mid X>0],\end{aligned}}} which gives us that ${\displaystyle \mathbf {E} [T(n)]={\frac {1}{1-p}}+\mathbf {E} [T(n-X)\mid X>0]}$. For the conditional expectation, we have {\displaystyle {\begin{aligned}&\mathbf {E} [T(n-X)\mid X>0]\\=&\mathbf {E} [\,\mathbf {E} [T(n-X)\mid X]\mid X>0]\\\leq &\mathbf {E} \left[\int _{c}^{n-X}{\frac {1}{\mu (x)}}\,\mathrm {d} x\,{\Big |}\,X>0\right]&{\text{(induction hypothesis)}}\\=&\mathbf {E} \left[\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x-\int _{n-X}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x\,{\Big |}\,X>0\right]\\=&\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x-\mathbf {E} \left[\int _{n-X}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x\,{\Big |}\,X>0\right]\\\leq &\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x-\mathbf {E} \left[\int _{n-X}^{n}{\frac {1}{\mu (n)}}\,\mathrm {d} x\,{\Big |}\,X>0\right]&{\text{(}}\mu (x){\text{ is non-degreasing)}}\\=&\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x-{\frac {1}{\mu (n)}}\mathbf {E} [X\mid X>0].\end{aligned}}} On the other hand, due to the total expectation, we have ${\displaystyle \mathbf {E} [X_{n}]=\mathbf {E} [X_{n}\mid X_{n}=0]\Pr[X_{n}=0]+\mathbf {E} [X_{n}\mid X_{n}>0]\Pr[X_{n}>0]=(1-p)\mathbf {E} [X_{n}\mid X_{n}>0],}$ which gives us that ${\displaystyle \mathbf {E} [X\mid X>0]={\frac {\mathbf {E} [X_{n}]}{1-p}}\geq {\frac {\mu (n)}{1-p}}}$. Substituting this in the above inequality gives us that {\displaystyle {\begin{aligned}\mathbf {E} [T(n)]&={\frac {1}{1-p}}+\mathbf {E} [T(n-X)\mid X>0]\\&\leq {\frac {1}{1-p}}+\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x-{\frac {1}{\mu (n)}}\cdot {\frac {\mu (n)}{1-p}}\\&=\int _{c}^{n}{\frac {1}{\mu (x)}}\,\mathrm {d} x.\end{aligned}}}
${\displaystyle \square }$

## Applications of Karp-Upfal-Wigderson bound

#### Random QuickSelect

As shown above, the number ${\displaystyle T(n)}$ of recursive calls in a running of RandomQS satisfies the recurrence:

${\displaystyle T(n)={\begin{cases}1+T(n-X(n))&n>1\\0&n=1\end{cases}}}$

where ${\displaystyle X(n)}$ is a random variable representing the number of elements in the input ${\displaystyle S}$ which are thrown away in the current call.

${\displaystyle X(n)={\begin{cases}n-|S_{1}|&{\text{if }}|S_{1}|\geq k-1\\|S_{1}|&{\text{if }}|S_{1}|

Recall that ${\displaystyle S_{1}=\{y\in S\mid y\leq x\}}$ is the number of elements in the current input ${\displaystyle S}$ no larger than the pivot ${\displaystyle x}$ and ${\displaystyle x}$ is uniformly chosen from ${\displaystyle S}$ at random. It is easy to see that no matter for what ${\displaystyle k}$ it holds that ${\displaystyle X(n)\geq \min(m,n-m)}$ where ${\displaystyle m}$ is uniformly random over ${\displaystyle \{0,1,\ldots n-1\}}$. An easy calculation gives us that ${\displaystyle {\textbf {E}}[X(n)]\geq {\frac {n}{4}}}$. By the Karp-Upfal-Wigderson bound, we have

${\displaystyle \mathbf {E} [T(n)]\leq \int _{1}^{n}{\frac {4}{x}}\,\mathrm {d} x=4\ln n.}$

#### Skip List

As discussed above, the number of levels of a Skip List for ${\displaystyle n}$ elements is given by ${\displaystyle T(n)}$ satisfying that

${\displaystyle T(n)={\begin{cases}1+T(n-X(n))&n>0\\0&n=0\end{cases}}}$

where ${\displaystyle X(n)}$ is the number of TAILs when independently flip ${\displaystyle n}$ biased coins, where each coin turns HEAD with probability ${\displaystyle p}$. Clearly ${\displaystyle X(n)}$ follows the binomial distribution ${\displaystyle B(n,1-p)}$, and its expectation is ${\displaystyle \mathbf {E} [X(n)]=(1-p)n}$.

By the Karp-Upfal-Wigderson bound, we have

${\displaystyle \mathbf {E} [T(n)]\leq \int _{0}^{n}{\frac {1}{(1-p)x}}\,\mathrm {d} x={\frac {1}{1-p}}(\ln n-\ln 0).}$

Here the ${\displaystyle \ln 0}$ will cause a problem. We can fix the problem by observing that for integral ${\displaystyle n}$, the expectation ${\displaystyle \mathbf {E} [X(n)]}$ is actually

${\displaystyle \mathbf {E} [X(n)]=(1-p)n=(1-p)\lceil n\rceil .}$

Thus the real Karp-Upfal-Wigderson bound for this process is

${\displaystyle \mathbf {E} [T(n)]\leq \int _{0}^{n}{\frac {1}{(1-p)\lceil x\rceil }}\,\mathrm {d} x={\frac {1}{1-p}}\sum _{k=1}^{n}{\frac {1}{k}}={\frac {H_{n}}{1-p}},}$

where ${\displaystyle H_{n}}$ is the harmonic number.

#### Coupon collector

We have represented the number of trials in a coupon collector game with ${\displaystyle n}$ coupons as ${\displaystyle T(n)}$ defined by the following recursion

${\displaystyle T(k)={\begin{cases}1+T(k-X(k))&k>0\\0&k=0\end{cases}}}$

where ${\displaystyle X(k)}$ is the number of trials taken when there are exactly ${\displaystyle k}$ coupons remaining to collect, which we know satisfy that

${\displaystyle X(k)={\begin{cases}1&{\text{with probability }}{\frac {k}{n}},\\0&{\text{with probability }}1-{\frac {k}{n}}.\end{cases}}}$

Thus ${\displaystyle \mathbf {E} [X(k)]={\frac {k}{n}}={\frac {\lceil k\rceil }{n}}}$. By the Karp-Upfal-Wigderson bound, we have

${\displaystyle \mathbf {E} [T(n)]\leq \int _{0}^{n}{\frac {n}{\lceil x\rceil }}\,\mathrm {d} x=n\sum _{k=1}^{n}{\frac {1}{k}}=nH_{n}.}$

#### Geometric distribution

We know that a geometric random variable can be described as a ${\displaystyle T(1)}$ defined as follows

${\displaystyle T(1)=1+T(1-X(1))}$ and ${\displaystyle T(0)=0}$

where ${\displaystyle X(1)\in \{0,1\}}$ is a Bernoulli random variable being 1 with probability ${\displaystyle p}$. Then ${\displaystyle \mathbf {E} [X]=p}$. By the Karp-Upfal-Wigderson bound, we have

${\displaystyle \mathbf {E} [T(1)]\leq \int _{0}^{1}{\frac {1}{p}}\,\mathrm {d} x={\frac {1}{p}}.}$