GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Analyze the worst case from repeatedly extreme pivots

4 questions solved the same way, newest first. Back to Algorithms

How these are solved

The operative mechanism is repeatedly obtaining a1-versus(n−1) split, producing a decrement recurrence with linear work per level.

1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 14, GATE 2015: For n≥2, choose the worst-case quicksort recurrence; c is constant. A T(n)=2T(n/2)+cn; B T(n)=T(n−1)+T(1)+cn; C T(n)=2T(n−1)+cn; D T(n)=T(n/2)+cn.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Worst-case pivots repeatedly leave one side with nearly every remaining element. Treat the constant-size side's cost as O(1), yielding T(n)=T(n−1)+Θ(n). Unrolling accumulates n+(n−1)+…+2=Θ(n²). A represents balanced splitting, C duplicates the large subproblem, and D omits one half of a balanced sort. Printed B uses T(1) for constant work.

Solution: The most unbalanced quicksort split leaves one nontrivial subproblem of size n−1 and constant base-case work, plus cn partition work. Hence the listed recurrence T(n)=T(n−1)+T(1)+cn, B, gives Θ(n²).

Original paper, page 7 · Answer key

1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 14, GATE 2014: Quicksort on n elements always selects the element at the central array index as pivot. What is the tightest listed worst-case upper bound? A O(n²); B O(n log n); C Θ(n log n); D O(n³).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step 1: A central position in an array is not necessarily the median value. An adversarial ordering can place the smallest or largest current value at each selected pivot position. Step 2: Partitioning then leaves subproblems of sizes 0 and n−1 while doing Θ(n) work. The recurrence is T(n)=T(n−1)+Θ(n). Step 3: Summing the partition costs gives Θ(n+(n−1)+…+1)=Θ(n²). Therefore A is the tightest offered upper bound; O(n³) is true but weaker.

Original paper, page 56 · Answer key

1 mark · MCQ
Report issue

Worst case of randomized quicksort

Searching, Sorting & Selection

Question 1.14, GATE 2001: Quicksort selects each pivot randomly. What is its worst-case sorting complexity on n values: O(n), O(n log n), O(n²), or O(n!)?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Random pivot selection improves the expected split quality, but it does not make extreme pivots impossible. A sequence selecting the minimum or maximum at every partition still has nonzero probability.

Step 2: That sequence performs(n−1)+(n−2)+…+1=n(n−1)/2 partition comparisons, yielding Θ(n²).

Thus C is the tight worst-case bound. Θ(n log n) is the expected runtime over the random choices, not the maximum possible runtime of a particular execution.

Original paper, page 2

Descriptive
Report issue

A worst input for last-pivot quicksort

Searching, Sorting & Selection

Question 3(iv), GATE 1992: Quicksort always uses the final element as pivot. Give a worst-case permutation of distinct keys 1, …, n.
Question 3(iv), GATE 1992: Quicksort always uses the final element as pivot. Give a worst-case permutation of distinct keys 1, …, n.
Tap to enlarge
Answer & solution

Answer: Ascending order 1, 2, …, n under the standard partition implementation (no official key was published)

On ascending input, the last pivot is the maximum. Partitioning places every other element on one side, creating subproblem sizes n−1 and 0. If the standard partition preserves the ascending order of that remaining region, the same imbalance repeats. Comparisons sum to (n−1)+(n−2)+…+1=n(n−1)/2, which is Θ(n²). Reverse order is another common worst-case input. The statement concerns ordinary end-pivot partitioning, not a modified algorithm that deliberately rearranges or selects pivots differently after inspecting the data.

Original paper, page 5