59 59 votes Let $P$ be a quicksort program to sort numbers in ascending order. Let $t_{1}$ and $t_{2}$ be the time taken by the program for the inputs $\left[1 \ 2 \ 3 \ 4\right]$ and $\left[5 \ 4 \ 3 \ 2 \ 1\right]$, respectively. Which of the following holds? $t_{1} = t_{2}$ $t_{1} > t_{2}$ $t_{1} < t_{2}$ $t_{1}=t_{2}+5 \log 5$ Algorithms gate1987 algorithms sorting quick-sort + – Misbah Ghaya 24.2k views answer comment Share Follow Print See all 12 Comments 12 12 Comments reply Show 9 previous comments Rish@bh_shukl@ commented Nov 10, 2025 reply Follow flag @Ujjwal_Nikam can we say that t1<t2 because t2 has more swaps than t1? 0 0 replyShare Ujjwal_Nikam commented Nov 10, 2025 reply Follow flag @Rishabh1006 Yes,bcz reverse-sorted i/p causes more swaps and deeper partitioning than sorted i/p in quicksort. 0 0 replyShare Princepavan_2003 commented Aug 6 reply Follow flag https://gateoverflow.in/2744/gate-cse-1996-question-2-15 similar question 0 0 replyShare Please log in or register to add a comment.
Best answer 78 78 votes Actually, in both the cases, it will take $O(n^{2})$ time for partition algorithm and $T(n-1)$ time for subproblem. As $n$ is the number of inputs and in the $2^{\text{nd}}$ case inputs are $5($greater than $1^{\text{st}}$ one that is $4),t_{1}<t_{2}.$ Correct Answer: C. Rohan Ghosh answered Jun 25, 2015 • edited Apr 15, 2021 by Lakshman Bhaiya Rohan Ghosh comment Share Follow See all 16 Comments 16 16 Comments reply Show 13 previous comments Shubhodeep commented Nov 6, 2022 reply Follow flag Running time of an algorithm depends on various factors such as processor speed etc. So it cannot be definitely said.. Whether t1<t2 as both P1[1234] and P2[4321] are worst cases for quick sort Check the best answer in this question: https://gateoverflow.in/8480/gate-cse-2015-set-3-question-27 1 1 replyShare Shivani Shukla commented Nov 7, 2022 reply Follow flag Understood . Thankyou @Shubhodeep 1 1 replyShare pavansan commented Jan 8, 2025 reply Follow flag yes i also thought the same 0 0 replyShare Please log in or register to add a comment.
18 18 votes In this questions ,they have asked the running time and not number of comparisons or swaps. Time complexity with depend on n. Since ,both the inputs [1,2,3,4] and [5,4,3,2,1] are already sorted, so both take O(n^2) time. (a) is correct. gshivam63 answered Jun 4, 2016 gshivam63 comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Manu Shaurya commented May 7, 2019 reply Follow flag you're right @Abhishek Gupta 1 1 1 replyShare Ritik gupta commented May 3, 2021 i edited by Ritik gupta May 4, 2021 reply Follow flag The time complexity and running time are two different things altogether.Time complexity is a complete theoretical concept related to algorithms, while running time is the time a code would take to run, not at all theoretical. Two algorithms may have the same time complexity, say O(n²), but one may take twice as much running time as the other one. time taken by program ≠ time complexity of program time taken by program = run time of a program Stackoverflow link 3 3 replyShare Princepavan_2003 commented Aug 6 reply Follow flag if t1=1, 2,3,4 t2=4, 3,2,1 both t1, t2 takes same time Since t2 has more inputs than t1, so, t2 takes more comparisons than t1. So, option C is correct 0 0 replyShare Please log in or register to add a comment.
4 4 votes It will be option A.t1 = t2 ,if the list is already sorted in ascending,descending or even all elements in the list are same (all elements identical) it will be have worst case partion for quicksort and complexity will be O(n^2). Surajit answered Dec 20, 2016 Surajit comment Share Follow See 1 comment 1 1 comment reply Manu Shaurya commented May 7, 2019 reply Follow flag The time complexity is going to be same, but not the running time due to the size of inputs. 0 0 replyShare Please log in or register to add a comment.
2 2 votes In the ascending order thing we don't have to swap any element and we just have to compare with the remaining elements.Where as in descending order things we have to compare as well as SWAP the element.The time complexity of both the algorithm will be O(n2)but the time taken for descending order will be greater than the time taken for the ascending order. nobodysomebody answered Aug 30, 2025 nobodysomebody comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes They said that time taken by input 1 and 2 are t1 and t2 not time complexity. So we consider value here. vermamayank564 answered Aug 14, 2017 vermamayank564 comment Share Follow See 1 comment 1 1 comment reply sanjeet24 commented Jul 26, 2023 reply Follow flag Time complexicity in both the case will be equal, because both will take worst time to compute ie O(n^2). 0 0 replyShare Please log in or register to add a comment.
0 0 votes in this question they have asked about time taken not time complexity . they wanted to compare time exact time consumed between t1 and t2 inputs . even . if we take a pivot which yeald you to O(nlogn) at best take . the comparison would be : k*(4*log(4)) time for t1 input < k*(5*log(5)) time for t2 input . k depends on system on which algorithm is runned and also on specific steps and comparisons . since we are running on the same quicksort algorithm . it maximum depend on system . now let compare for worst case . it will happen when pivot is placed in last position everytime : T(n) = t(n-1)+n = O(n^2). on comparing time consumed on worst case : k*(4^2) < k*(5^2) , k depends on system on which algorithm is runned and also on specific steps and comparisons . since we are running on the same quicksort algorithm . it maximum depend on system . in both cases t2>t1 . therefore answer is Option C . జై బాబు అనాలి answered Jun 11 • edited Jun 16 by జై బాబు అనాలి జై బాబు అనాలి comment Share Follow 0 reply Please log in or register to add a comment.