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?$O(n)$$O(n \log n)$$O(n^2)$$O(n!)$ Algorithms gatecse-2001 algorithms sorting time-complexity easy quick-sort + – Kathleen 20.7k views answer comment Share Follow Print See 1 comment 1 1 comment reply smsubham commented Feb 2, 2018 reply Follow flag https://cs.stackexchange.com/questions/35994/why-does-randomized-quicksort-have-on-log-n-worst-case-runtime-cost 2 2 replyShare Please log in or register to add a comment.
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})$ 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. 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. Manu Thakur answered Aug 20, 2017 • edited May 12, 2021 by soujanyareddy13 Manu Thakur comment Share Follow See all 13 Comments 13 13 Comments reply Show 10 previous comments Argharupa Adhikary commented Dec 20, 2022 reply Follow flag @Abhrajyoti00(n-1)th Time: 2/(n−1) [ Here probability will be 1, because getting either largest or smallest out of two no. has prob of 1 for sure]nth Time: Probability will be 1 [obvious]2 nd time pivot selection : 2/(n – 1)3rd time: 2/(n – 2)(n- 1) th time: 2/ (n – (n – 2)) = 2/2 = 1n th time pivot selection is not required.In this case, maximum only (n-1) time pivot selection is required to sort the array.Thus total probability: 2∗2∗2...[n-1 times] /(n∗(n−1)∗(n−2)∗…∗1) = (2^(n-1))/ n! 4 4 replyShare Abhrajyoti00 commented Dec 20, 2022 reply Follow flag Yes @Argharupa Adhikary. Although it’s not wrong to take n th time probability. But it’s not reqd. as it is always the smallest/largest. 0 0 replyShare manishankarkanrar commented Jul 26, 2024 reply Follow flag Salute to You bro 1 1 replyShare Please log in or register to add a comment.
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 Vikrant Singh answered Dec 13, 2014 • edited Jan 10, 2018 by Puja Mishra Vikrant Singh comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments anonymous commented Jun 27, 2016 reply Follow flag Randomized quick sort picks the pivot randomly so in best case and avg case it gives O(nlogn) time complexity but stil in worst case there is a chance that it may select smallest element as pivot..so O(n^2) in worst case 1 1 replyShare asu commented Aug 2, 2016 reply Follow flag @arjun sir i did not get the point...."expected num of comparision" 0 0 replyShare pC commented Dec 27, 2016 i edited by pC Dec 27, 2016 reply Follow flag In Random Quick Sort All these cases may come . ie ' In worst case, we may pick pivot elements in the increasing / decreasing order Hence it is $O(n^{2})$ and $\Omega(n \log n)$ 1 1 replyShare Please log in or register to add a comment.
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). Chirag Shilwant answered Dec 10, 2019 Chirag Shilwant comment Share Follow 0 reply Please log in or register to add a comment.
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) sanyam53 answered Oct 27, 2016 sanyam53 comment Share Follow 0 reply Please log in or register to add a comment.