QUICKSORT(A,p,r)
if p < r
q = PARTITION(A,p,r)
QUICKSORT(A,p,q-1)
QUICKSORT(A,q+1,r)
PARTITION(A,p,r)
x = A[r]
i = p-1
for j = p to r-1
if A[j] <= x
i = i + 1
exchange A[i] with A[j]
exchange A[i+1] with A[r]
return i + 1

Here are some key observations about the behavior of Quicksort:
Here is a probabilistic argument that estimates the total number of comparisons.
Introduce the random variable

In terms of this variable, the random variable that represents the total number of comparisons is

We want to compute the expected value of X, which will yield the total number of comparisons in Quicksort.

E[Xi,j] is just the probability that xi gets compared with xj. What is that probability? It is easiest to compute the probability that xi does not get compared with xj: that will happen if any of the numbers between xi and xj gets chosen as pivot first. Assuming that every number has an equal probability of being chosen first, that probability is simply

Thus

and


