GATE Computer Science · Programming, Data Structures, and Algorithms · easy
For Quicksort using deterministic last-element pivoting on an array of n distinct elements that is already sorted in strictly ascending order, what is the worst-case time complexity?
Published · Last updated · checked before publication — how we verify questions
Options
Answer: C. Theta(n^2) because each partition produces subproblems of sizes n - 1 and 0
With last-element pivoting on an array already sorted in ascending order, the pivot is always the maximum of the current subarray. Partitioning (Lomuto or Hoare style) compares it against every other element, costing Theta(n) work, and leaves all remaining n - 1 elements on one side and nothing on the other. The recurrence is T(n) = T(n - 1) + Theta(n), which unrolls to n + (n - 1) + ... + 1 = Theta(n^2). The recursion depth is also n, not log n. The key concept is that Quicksort's running time depends on how balanced the splits are, and a deterministic pivot rule has fixed bad inputs. For last-element or first-element pivots, already sorted or reverse sorted input is the classic worst case. The common trap is assuming sorted input is the easy case, as it is for insertion sort, or assuming divide and conquer always gives n log n. Randomized or median-of-three pivoting avoids this particular bad input.
Why the other options are wrong
- A. Theta(n) because the partition scan sees zero inversions and stops after one pass — Theta(n) is the best case for insertion sort on sorted input, not for Quicksort. Quicksort does not detect sortedness. The partition step still compares the pivot with every other element and then recurses on a subarray of size n - 1, so the total work is quadratic.
- B. Theta(n log n) because divide-and-conquer sorting splits arrays into equal halves — Theta(n log n) is Quicksort's best and average case, which needs reasonably balanced splits. Divide and conquer does not guarantee equal halves, because the split depends on the pivot. Here the pivot is the maximum, so the split is n - 1 and 0, not n/2 and n/2.
- D. Theta(log n) because the recursion stack depth is logarithmic — Theta(log n) describes the recursion depth only in the balanced case. Here the depth is n, and every level does linear partitioning work. Any comparison sort also needs at least n - 1 comparisons just to look at every element, so Theta(log n) cannot be the running time.
More Programming, Data Structures, and Algorithms questions
- Consider the recurrence relation T(n) = 4T(n / 2) + n^2 for n > 1, with T(1) = 1. Using the Master Theorem, what is…
- What is the length of the Longest Common Subsequence (LCS) of the two strings X = 'ABCBDAB' and Y = 'BDCABA'?
- Which two statements accurately compare Dijkstra's single-source shortest-path algorithm and the Bellman-Ford…
- A message uses five characters A, B, C, D, and E with frequencies 5, 9, 12, 13, and 16 respectively. Using optimal…
All Programming, Data Structures, and Algorithms questions and the topic overview →
GATE is a mark of the Indian Institute of Science and the Indian Institutes of Technology. PrepIQ Hub is not affiliated with, endorsed by, or sponsored by the GATE Committee.