December 31st, 2007, 5:52 pm
QuoteOriginally posted by: smiesQuicksort is O(n log n), but for median selection (or, for that matter, any element selection), once you've partitioned the data you only need to look at one half of it each time, so you average n + n/2 + n/4 + ... operations, which is O(n).That's still not O(n) in worst case, is it? There's a way to deterministically get worst case O(n) -- First, break the n elements into (n/5) chunks of 5 each. Find the median in each, find the median of these n/5 medians.Use that as your pivot -- this is guaranteed to have at least 3:7 or better partitioning of the list. (Why? Hint: medians)Process the partition you are interested in, it is at most as big as (7n/10)Recurrence relation for the complexity is T(n) = T(n/5) + T(7n/10) + O(1)Note that the problem size has decreased by a factor of 1/10, giving T(n) = O(n).
Last edited by
ArthurDent on December 30th, 2007, 11:00 pm, edited 1 time in total.