LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

When does quick-sort need $O(n^2)$ time?

When every pivot is the smallest or the largest element of its sequence: one of $L$ and $G$ is then empty and the other has $n - 1$ elements, so the partitions cost $n + (n-1) + \dots + 2 + 1 = \frac{n(n+1)}{2}$, which is $O(n^2)$.

The sorted input 1 to 7 quick-sorted twice: the first element as pivot gives a chain of six levels, the middle element gives two levels

* Same input, same algorithm: only the pivot rule differs. *

The running time of quick-sort is the total length of all its partitions. With an extreme pivot each call shrinks the problem by one element only, the pivot itself, so the tree degenerates into a chain of depth $n - 1$ and the lengths add up like in selection-sort.

The trap is that this case is not exotic. Taking the first or the last element as pivot hits it exactly on input that is already sorted, or sorted backwards, and such input is common. The chain has a second cost: one stack frame per level, so a recursive implementation can run out of stack on a large sorted array long before the $n^2$ time becomes the problem.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026