GATE | GATE-CS-2006 | Question 52
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)
(C) O(n Logn Logn)
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
This solution is contributed by Parul Sharma.
Quiz of this Question
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.