Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GATE | GATE-CS-2006 | Question 52

  • Last Updated : 28 Jun, 2021

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:

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :