You are on page 1of 1

How to make Quicksort worst-case linearithmic?

Quicksort can be made in the worst case (nlgn) by choosing


the pivot in the same way as in the worst case linear selection for
the i-th smallest key. Recall that the pivot there was defined equal to
the median of the sequence with n/5 elements constructed from
medians of 5-element groups. Using the worst-case linear algorithm
to select the median of the sequence of medians, the work needed to
choose the pivot will take linear time.
The worst case for Quicksort at a pivot chosen in this a way will
occur when the partition produces the most unbalanced subranges
of sizes n/4 and 3n/4. So, the recurrence for the worst case is:
W(n)=W(n/4)+W(3n/4)+ (n),
which yields W(n)=(n lgn) (Why?).
Note: In this way we improve the worst case for Quicksort, but
lose in the average behavior since it becomes cnlgn with c
significantly exceeding 1.386.

A. Kostanyan
1

You might also like