edited by
20,696 views
59 59 votes

Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using Randomized quicksort?

  1. $O(n)$
  2. $O(n \log n)$
  3. $O(n^2)$
  4. $O(n!)$

4 Answers

Best answer
70 70 votes

Correct Option: C
There are two cases, when Randomized Quick Sort will result into worst case time complexity of $O(n^{2})$

  1. When all elements are same in the input array, Partition algorithm will divide input array in two sub-arrays, one with $n-1$ elements and second with $0$ element. There is an assumption here that, we are using the same partition algorithm without any modification.
     
  2. If the randomised pivot selector happens to select the smallest or largest element N times in a row, we will get the worst possible  performance. Though the probability of this particular case is about $\frac{2^{n-1}}{n!}.$

PS: Option D is also correct here as $n^2 = O(n!)$ though $(C)$ is a better choice.

edited by
54 54 votes
In worst case, we may pick pivot elements in the increasing order (input also given in sorted order) which will result in running time of O($n^{2}$)

Both the deterministic and randomized quicksort algorithms have the same best-case running times of O($nlogn$) and the same worst-case running times of O(n$^{2}$).The difference is that with the deterministic algorithm, a particular input can elicit that worst-case behavior.  With the randomized algorithm, however, no input can always elicit the worst-case behavior.  The reason it matters is that, depending on how partitioning is implemented, an input that is already sorted--or almost sorted--can elicit the worst-case behavior in deterministic quicksort.

source: Thomas Coremen

Ans. C
edited by
3 3 votes
Randomized quicksort has expected time complexity as O(nLogn), but worst case time complexity remains same. In worst case the randomized function can pick the index of corner element every time.

Hence O(n^2).
0 0 votes

The running time of Randomized QUICKSORT when all elements of array A have the same value will be equivalent to the worst case running of QUICKSORT since no matter what pivot is picked, QUICKSORT will have to go through all the values in A. And since all values are the same, each recursive call will lead to unbalanced partitioning.

Thus the recurrence will be:

T(n)=T(n−1)+Θ(n)

T(n)=Θ(n2)

Answer:
Position:
Show:

Related questions

8 8 votes
6 6 answers
3.8k
3.8k views
Arjun asked Feb 27, 2025
3,829 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
61 61 votes
9 answers 9 answers
29.2k
29.2k views
go_editor asked Sep 26, 2014
29,236 views
Let $P$ be quicksort program to sort numbers in ascending order using the first element as the pivot. Let $t_1$ and $t_2$ be the number of comparisons made by P for the i...
90 90 votes
10 answers 10 answers
43.8k
43.8k views
go_editor asked Sep 28, 2014
43,833 views
You have an array of $n$ elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the...
20 20 votes
3 answers 3 answers
6.0k
6.0k views
Misbah Ghaya asked Nov 2, 2015
6,029 views
Consider the quick sort algorithm on a set of $n$ numbers, where in every recursive subroutine of the algorithm, the algorithm chooses the median of that set as the pivot...