edited by
31,955 views
74 74 votes

In quick-sort, for sorting $n$ elements, the $\left(n/4\right)^{th}$ smallest element is selected as pivot using an $O(n)$ time algorithm. What is the worst case time complexity of the quick sort?

  1. $\Theta(n)$

  2. $\Theta(n \log  n)$

  3. $\Theta(n^2)$

  4. $\Theta(n^2 \log  n)$

8 Answers

Best answer
71 71 votes

Answer is B.

$T(n)= O(n)$ pivot selection time $ + T(n/4 - 1) + T(3n/4)$

which'll give $\Theta\left(n \log n\right)$.

Pivot selection complexity is given in questions. Pivot being the $(n/4)$th smallest element, once it is found, we have two sub arrays- one of size $(n/4 - 1)$ and other of size $(3n/4)$ and for both of these we solve recursively.

edited by
13 13 votes
CLRS book mentions that any split of constant proportionality produces a time complexity for quicksort which is same as average case, i.e Θ( n log n ). Here as we know the split is n/4 : 3n/4...we know time complexity will remain Θ( n log n ) .
4 4 votes

Note:

$4^{th}$ smallest will go to $4^{th}$ place.

$7^{th}$ smallest will go to $7^{th}$ place.

$\dfrac{n}{4}^{th}$ smallest will go to $\dfrac{n}{4}^{th}$ place.

$\underbrace{\left | \dfrac{n}{4}-1 \right |  \fbox{$\dfrac{n}{4}^{th}$}}$ $\left | n-\dfrac{n}{4} \right |$

$\dfrac{n}{4}\ elements$


$\underbrace{O(n)}$      

$\dfrac{n}{4}^{th}$

smallest element

$+$

$\underbrace{O(1)}$

Swap with

last element

$+$

$\underbrace{O(n)}$

partition

algo

$+$

$T\left(\dfrac{n}{4}-1\right)+T\left(n-\dfrac{n}{4}\right)$


$T(n)=O(n)+O(1)+O(n)+ T\left(\dfrac{n}{4}\right)+T\left(\dfrac{3n}{4}\right)$

$T(n)=O(n)+ T\left(\dfrac{n}{4}\right)+T\left(\dfrac{3n}{4}\right)$

$T(n)=O(n\ logn)$

3 3 votes
Basically we have worst case in quick sort when all elements are same or sorted. If they are all same then we can't find the (n/4)th smallest element in it.

But if the value are 1 2 3 4 5.

We take pivot 1 here.  Then 2, 3, 4, and 5.

So, no of comparison is n-1 + n-2 + ... + 1.

So n^2 is complexity for worst case which is sorted
2 flags:
✌ (jayy_patel “wrong answer”)
✌ (solar_raven)
Answer:
Position:
Show:

Related questions

43 43 votes
2 answers 2 answers
35.5k
35.5k views
Kathleen asked Sep 22, 2014
35,509 views
What is the number of swaps required to sort $n$ elements using selection sort, in the worst case?$\Theta(n)$$\Theta(n \log n)$$\Theta(n^2)$$\Theta(n^2 \log n)$
56 56 votes
4 answers 4 answers
22.5k
22.5k views
go_editor asked Apr 23, 2016
22,452 views
A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences $X[m]$ and $Y[n]$ of lengths ...
39 39 votes
3 answers 3 answers
13.7k
13.7k views
Kathleen asked Sep 22, 2014
13,709 views
A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences $X[m]$ and $Y[n]$ of lengths ...
31 31 votes
5 answers 5 answers
14.3k
14.3k views
Kathleen asked Sep 22, 2014
14,346 views
Consider the following graph:Which one of the following is NOT the sequence of edges added to the minimum spanning tree using Kruskal’s algorithm?$\text{(b, e) (e, f) (a,...