every time we are finding median in O(n) times then solving the 2 half array recurcively...

therefore recurrence relation:

T(n) = 2T( n / 2) + O(n)..

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+18 votes

The median of $n$ elements can be found in $O(n)$ time. Which one of the following is correct about the complexity of quick sort, in which median is selected as pivot?

- $\Theta (n)$
- $\Theta (n \log n)$
- $\Theta (n^{2})$
- $\Theta (n^{3})$

+23 votes

Best answer

+17

every time we are finding median in O(n) times then solving the 2 half array recurcively...

therefore recurrence relation:

T(n) = 2T( n / 2) + O(n)..

+2

What if the array is already sorted? or All the elements in the array are same?

In that case the complexity will be O(n^2) ?

In that case the complexity will be O(n^2) ?

+1

+2

Here input will be like this

7 6 2 1 3 5 4

So, median element we will get at last.

median means middle element of sorted list

7 6 2 1 3 5 4

So, median element we will get at last.

median means middle element of sorted list

+1

@Srestha this example will take O(n^2). I think O(n^2) will be the right.

This e.g break the equation T(n)=T(n-1)+T(0)+ O(n)

This e.g break the equation T(n)=T(n-1)+T(0)+ O(n)

+20 votes

To people having doubt on what will be the time complexity when all input values are equal. I had the same doubt for few days and after spending some time on this question here is what i found:

1.There are two standard partition procedure of quicksort, one is lomuto partition which is given in topic of quicksort in cormen in which last element is chosen as pivot and both i and j starts from starting index, second one is hoare's partition which is the original quicksort partition procedure given by hoare who gave quicksort algorithm, this partition procedure is given in problems section of cormen, in this first element is chosen as pivot but i and j starts from opposite ends.

2.Now if u consider lomuto partition then no matter what pivot choosing strategy u follow, be it median or middle or anything the worst case will always be O(n^2) because of input type being all elements with equal values. but if we consider hoare partition and we choose good pivot like median then if all elements are same then we get equal partition so in this worst case can be guaranteed O(nlogn).

here is the reference to geeks for geeks comparing hoare vs lomuto partition: http://www.geeksforgeeks.org/hoares-vs-lomuto-partition-scheme-quicksort/

what i think is in two gate questions asking time complexity when pivot is median and in other one pivot is 4th smallest element they might be considering hoare partition only.

- All categories
- General Aptitude 1.3k
- Engineering Mathematics 5.5k
- Digital Logic 2.1k
- Programming & DS 4k
- Algorithms 3.4k
- Theory of Computation 4.2k
- Compiler Design 1.6k
- Databases 3.1k
- CO & Architecture 2.7k
- Computer Networks 3.1k
- Non GATE 1.1k
- Others 1.4k
- Admissions 501
- Exam Queries 449
- Tier 1 Placement Questions 19
- Job Queries 62
- Projects 12

38,079 questions

45,572 answers

132,069 comments

49,045 users