随机算法 (Fall 2011)/Randomized Quicksort
Given as input a set of numbers, we want to sort the numbers in in increasing order. One of the most famous algorithm for this problem is the Quicksort algorithm.
- if do:
- pick an as the pivot;
- partition into , , and , where all numbers in are smaller than and all numbers in are larger than ;
- recursively sort and ;
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 .
Algorithm: RandQSort
We consider the following randomized version of the quicksort.
- if do:
- uniformly pick a random as the pivot;
- partition into , , and , where all numbers in are smaller than and all numbers in are larger than ;
- recursively sort and ;
Analysis
Our goal is to analyze the expected number of comparisons during an execution of RandQSort with an arbitrary input . 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 denote the th smallest element in . Let be the random variable which indicates whether and are compared during the execution of RandQSort. That is:
Elements and 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 observation:
Claim 1: Every pair of and are compared at most once.
Therefore the sum of for all pair gives the total number of comparisons. The expected number of comparisons is . Due to Linearity of Expectation, . Our next step is to analyze for each .
By the definition of expectation and ,
We are going to bound this probability.
Claim 2: and 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.
Claim 3: If and are still in the same subset then all are in the same subset.
We can verify this by induction. Initially, itself has the property described above; and partitioning any with the property into and will preserve the property for both and . Therefore Claim 3 holds.
Combining Claim 2 and 3, we have:
Claim 4: and are compared only if one of is chosen from .
And apparently,
Claim 5: Every one of is chosen equal-probably.
This is because our RandQSort chooses the pivot uniformly at random.
Claim 4 and Claim 5 together imply:
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 and , initially are all in the same set (obviously!). During the execution of the algorithm, the set which containing are shrinking (due to the pivoting), until one of is chosen, and the set is partitioned into different subsets. We ask for the probability that the chosen one is among . So we really care about "the last" pivoting before is split. Formally, let be the random variable denoting the pivot element. We know that for each , with the same probability, and with an unknown probability (remember that there might be other elements in the same subset with ). The probability we are looking for is actually , which is always , provided that is uniform over . The conditional probability rules out the irrelevant events in a probabilistic argument. |
Summing all up:
is the th Harmonic number. It holds that
- .
Therefore, for an arbitrary input of numbers, the expected number of comparisons taken by RandQSort to sort is .