Open In App

GATE | GATE-CS-2006 | Question 52

Like Article
Like
Save
Share
Report

Suppose we have a O(n) time algorithm that finds median of an unsorted array.

Now consider a QuickSort implementation where we first find median using the above algorithm, then use median as 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:

When we choose median as pivot element then after the partition algorithm it will go in the middle of the array having half of the elements to left the left and half in the right.
Thus after partition algorithm the array will be divided into two equal parts of n/2 elements each.
Hence the resultant recurrence relation would be-
T(n) = O(n) (for selecting median) + O(n) (for partition) + T(n/2) + T(n/2)
T(n) = O(n) + 2T(n/2)
We can solve the above recurrence relation using master method

Reference:
https://en.wikipedia.org/wiki/Master_theorem

Same as http://geeksquiz.com/algorithms-searching-and-sorting-question-3/

This solution is contributed by Parul Sharma.

Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads