315 Notes, Friday, May 7, 2004

Divide and Conquer, cont'd.

Median selection

Think how efficient would QuickSort be if we could select the pivot in the middle of the sorted array! It turns out that it is possible to do it in linear time (how would using this instead of the random pivot impact the complexity of QuickSort?) Let us rename the problem: the element "in the middle" of sorted collection is called the median (hence the title.) Actually, the algorithm applies also to choosing an element with any ordinal (also other than n/2.)

A quick presentation of the algorithm: Compute the median elements of the consecutive m-tuples of the sequence (think: m=5, for instance). Recursively, find the median M of the sequence of medians and then its ordinal in the original sequence. Wlog., suppose that it is n/2 - k (how do we know that?) To complete the original task, we need to find (again, recursively) the k-th element among the elements greater than M. One can easily show that there are at most 3n/4 elements greater than M (also, at most that many elements smaller than M, so this applies to the case when M lands above the median, as well.) The total execution time of the algorithm is described by the recurrence

T(n) = T(n/5) + T(3n/4) + n. Its solution is O(n).