Suppose we have an O(n) time algorithm that finds the median of an unsorted array. Now consider a QuickSort implementation where we first find the median using the above algorithm, then use the median as a pivot. What will be the worst-case time complexity of this modified QuickSort?
(A)
O(n^2 Logn)
(B)
O(n^2)
(C)
O(n Logn Logn)
(D)
O(nLogn)
Answer: (D)
Explanation:
If we use the median as a pivot element, then the recurrence for all cases becomes T(n) = 2T(n/2) + O(n)
The above recurrence can be solved using Master method. It falls in case 2 of the master method.
So, the worst-case time complexity of this modified QuickSort is O(nLogn).
Quiz of this Question
Please comment below if you find anything wrong in the above post