42 42 votes Quick-sort is run on two inputs shown below to sort in ascending order taking first element as pivot$1, 2, 3, \dots n$$n, n-1, n-2, \dots, 2, 1$Let $C_1$ and $C_2$ be the number of comparisons made for the inputs (i) and (ii) respectively. Then, $C_1 < C_2$$C_1 > C_2$$C_1 = C_2$we cannot say anything for arbitrary $n$ Algorithms gate1996 algorithms sorting normal quick-sort + – Kathleen 17.5k views answer comment Share Follow Print See all 19 Comments 19 19 Comments reply Show 16 previous comments Ujjwal_Nikam commented Nov 6, 2025 reply Follow flag Same number of comparisons: structure of recursion tree is identicalSwap behavior differs, but question only asks about comparisons 1 1 replyShare Prateek_Pandey commented Jan 17 reply Follow flag no, if sorted in decreasing order then it is going to give n(n-1/2 because in swap we check which element is smaller 0 0 replyShare Jayvijay Chauhan commented Apr 23 reply Follow flag similar question https://gateoverflow.in/80366/gate-cse-1987-question-1-xviii 0 0 replyShare Please log in or register to add a comment.
Best answer 46 46 votes Correct Option: C both are the worst cases of quick sort. (assuming pivot is either first or last element) is sorted in ascending order. is sorted in descending order. Gate Keeda answered Oct 9, 2014 • edited May 12, 2021 by soujanyareddy13 Gate Keeda comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Puja Mishra commented Aug 3, 2017 reply Follow flag explain anyone ... specifically abt the pivot element...... 0 0 replyShare Psnjit commented Jan 18, 2019 reply Follow flag If you consider like a tree then the no of comparison at each levels depends upon 'n' and if the pivot element is last in case of an already sorted array. Then no of levels = n. Thus no of comparison = n*n. In case of an unsorted array no of comparison considering logn levels= n*logn. So shouldn't c1>c2? (Assuming the last element is choosen as the pivot always) 0 0 replyShare maverick commented Oct 31, 2020 reply Follow flag If the same algorithm is used for running both arrays and the algorithm chooses the last element as the pivot, then the comparisons will be C1<C2 and vice versa if the pivot chosen is the first element. If the pivot chosen is the middle element, every time, then C1=C2. 0 0 replyShare Please log in or register to add a comment.
8 8 votes 1)ascending case==>Comparisons(C1)=(n-1)+(n-2)+(n-3)+.........................+2+1 =n(n-1)/2 =o(n^2) Swaps(S1)=1+1+1+............................................+1(upto n pass) =n =o(n) 2)Descending case==>Comparisons(C2)=(n-1)+(n-2)+(n-3)+....................................+1 =n(n-1)/2 =o(n^2) Swaps(S2)=(n)+(1)+(n-2)+(1)+(n-4)+...........(ascending and descending cases will come alternatively) =3n^2/4 =o(n^2) so C1=C2 and S1<S2 = talha hashim answered Jun 25, 2018 talha hashim comment Share Follow See all 4 Comments 4 4 Comments reply HeadShot commented Nov 27, 2018 i edited by HeadShot Nov 27, 2018 reply Follow flag @Mk Utkarsh i am not getting why that "single" swap required if array is sorted in ascending order, as pivot is placed correctly ( say left end element as pivot ) and all right side elements are larger and lhs are smaller ( though nothing is in lhs ).. So where single swap is needed ? are we swapping element with itself hence "1" swap ? 0 0 replyShare Vaishalikashyap commented May 26, 2020 reply Follow flag @talha hashim please explain how you are calculating swaps when input is in descending order? 0 0 replyShare sanjaysharmarose commented Aug 10, 2020 i edited by sanjaysharmarose Aug 10, 2020 reply Follow flag This is what I think: In ascending order : No. of comparisons = (n-1)+(n-2)+......+1 = O(n^2) No. of swappings = (n)+(n-1)+......+1 = O(n^2) [here every element will be swapped with itself ] In descending order : No. of comparisons = (n-1)+(n-2)+......+1 = O(n^2) No. of swappings = 1+1+1+......n times = O(n) [here only pivot element will be swapped] 0 0 replyShare Swarnava Bose commented Sep 22, 2023 reply Follow flag No.of swappings for ascending order will be O(n) because it is 1 + 1 + 1 + 1 + ... 0 0 replyShare Please log in or register to add a comment.
1 1 vote I think answer is C1>C2 because in ascending input left do 1 comparison and than stops and than right do all comparison until left=right. But in descending input left keeps going until left=right and right won't do any comparison because according to algorithm right start moving only after left finds a key which is larger than pivot which is not in the case of descending input. meivinay answered May 14, 2020 meivinay comment Share Follow See 1 comment 1 1 comment reply ras5014 commented Sep 17, 2021 reply Follow flag Thanks 0 0 replyShare Please log in or register to add a comment.
1 1 vote From my POV - As both are the Worst Cases for QuickSort and Question ask for the No. of Comparisons and Both have the equal No. of Elements So, the Answer is (C) C1=C2Please Correct me if I'm Wrong..... Shivamlohia answered Mar 2, 2025 • edited Mar 5, 2025 by Shivamlohia Shivamlohia comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Here is one solution. Hussain9660 answered Sep 19, 2024 Hussain9660 comment Share Follow 0 reply Please log in or register to add a comment.