7.3 A randomized version of quicksort

| < Day Day Up > |

## 7.3 A randomized version of quicksort

In exploring the average-case behavior of quicksort, we have made an assumption that all permutations of the input numbers are equally likely. In an engineering situation, however, we cannot always expect it to hold. (See Exercise 7.2-4.) As we saw in Section 5.3, we can sometimes add randomization to an algorithm in order to obtain good average-case performance over all inputs. Many people regard the resulting randomized version of quicksort as the sorting algorithm of choice for large enough inputs.

In Section 5.3, we randomized our algorithm by explicitly permuting the input. We could do so for quicksort also, but a different randomization technique, called random sampling, yields a simpler analysis. Instead of always using A[r] as the pivot, we will use a randomly chosen element from the subarray A[p ‥ r]. We do so by exchanging element A[r] with an element chosen at random from A[p ‥ r]. This modification, in which we randomly sample the range p,...,r, ensures that the pivot element x = A[r] is equally likely to be any of the r - p + 1 elements in the subarray. Because the pivot element is randomly chosen, we expect the split of the input array to be reasonably well balanced on average.

The changes to PARTITION and QUICKSORT are small. In the new partition procedure, we simply implement the swap before actually partitioning:

    RANDOMIZED-PARTITION(A, p, r)
    1  i ← RANDOM(p, r)
    2  exchange A[r] ↔ A[i]
    3  return PARTITION(A, p, r)

The new quicksort calls RANDOMIZED-PARTITION in place of PARTITION:

    RANDOMIZED-QUICKSORT(A, p, r)
    1  if p < r
    2     then q ← RANDOMIZED-PARTITION(A, p, r)
    3          RANDOMIZED-QUICKSORT(A, p, q - 1)
    4          RANDOMIZED-QUICKSORT(A, q + 1, r)

We analyze this algorithm in the next section.

Exercises 7.3-1

Why do we analyze the average-case performance of a randomized algorithm and not its worst-case performance?

Exercises 7.3-2

During the running of the procedure RANDOMIZED-QUICKSORT, how many calls are made to the random-number generator RANDOM in the worst case? How about in the best case? Give your answer in terms of Θ-notation.

| < Day Day Up > |