Randomized Quicksort
If you assume all permutations of the numbers are equally
likely when choosing the "divider" T (n) = Q
(n lg n) , we are in the average case. If we cannot assume all permutations
are equally likely, we must enforce it.
Random ( a, b ) returns integer , x, ' a
£ x £ b It is
implemented with a pseudorandom number generator. http://sprng.cs.fsu.edu
has lots of PRNG's.
The randomized quicksort algorithm uses i ¬
Random ( A, p, r ) to be the new pivot element.
Randomized-Partition ( A, p, r )
-
i ¬ Random ( p, r )
-
exchange A[p] « A[i]
-
return Partition ( A, p, r )
Randomized-Quicksort ( A, p, r )
-
If p < r
-
then q ¬
Randomized-Partition ( A, p, r )
-
Randomized-Quicksort ( A, p, q )
-
Randomized-Quicksort ( A, q+1, r )
|