Suppose we have an 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?
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.
If we use median as a pivot element, then for each iteration it takes 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 )
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 ) ) .