Median Quicksort

Suppose we have an O ( n ) O(n) algorithm that finds the median of an unsorted array of numbers. If we use the median as a pivot in a Quicksort implementation, what will be the worst case time complexity of the modified algorithm?

O ( log ( n ) ) O(\log(n)) O ( log log ( n ) ) O(\log\log(n)) O ( n ) O(n) O ( n log n ) O(n\log n)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Tom Engelsman
Oct 17, 2020

If we use median as a pivot element, then for each iteration it takes O ( n ) O(n) time to find median from given function and every time the array partitions in two subparts. So the recurrence equation becomes.

T ( n ) = 2 T ( n / 2 ) + O ( n ) T(n) = 2T(n/2) + O(n)

The above recurrence can be solved using Master Method. It falls in Case 2 of master method which results in complexity O ( n l o g ( n ) ) . \boxed{O(n \cdot log(n))}.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...