SKEDSOFT

Design Analysis Of Algorithm

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. As 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.

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[pr]. We do so by exchanging element A[r] with an element chosen at random from A[pr]. 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)

i ← RANDOM(p, r)

2  exchange A[r] ↔ A[i]

return PARTITION(A, p, r)

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

RANDOMIZED-QUICKSORT(A, p, r)

if p < r

2     then q ← RANDOMIZED-PARTITION(A, p, r)

3          RANDOMIZED-QUICKSORT(A, p, q - 1)

4          RANDOMIZED-QUICKSORT(A, q 1,