Worst case efficiency of quick sort over an array having n items can be…
2013
Worst case efficiency of quick sort over an array having n items can be improved by
- A.
Non-recursive method
- B.
Randomization
- C.
Recursive method
- D.
None of these
Attempted by 19 students.
Show answer & explanation
Correct answer: B
Concept
Quick sort partitions the array around a chosen pivot, then recurses on the two parts. Its running time is governed entirely by how BALANCED the partitions are, which depends on the pivot. A pivot that repeatedly splits the array into a 1-element part and an (n-1)-element part produces n levels of work of size n, giving an O(n2) cost; a pivot that splits near the middle gives roughly log n levels and O(n log n).
Why the formal worst case appears
With a FIXED deterministic pivot rule (e.g. always take the first or last element), a specific input — most famously an already-sorted or reverse-sorted array — forces the maximally unbalanced split at every level. Because that bad input is tied to the pivot rule, naturally ordered data (or an adversary) hits the O(n2) case every time.
How to improve it
Choose the pivot at RANDOM (randomization), so the pivot becomes independent of the input order. The improvement is about EXPECTED behaviour, and works as follows:
No single input ordering can be the worst case any longer, because the input can no longer be tailored to a fixed pivot rule — not even an already-sorted array.
For EVERY input the EXPECTED (average) running time is O(n log n); the probability of repeatedly hitting near-extreme pivots is vanishingly small for large n.
Strictly, an O(n2) worst case still EXISTS in theory — randomization does not change the formal upper bound — but its probability shrinks toward zero, so the quadratic blow-up is avoided in practice.
Cross-check / contrast
Storing the recursion iteratively with an explicit stack (a non-recursive version) changes only HOW pending sub-arrays are held; the comparisons and partition balance are identical, so the behaviour is unchanged.
Leaving it recursive is just the ordinary formulation of quick sort and does nothing about pivot quality.
Because one offered technique does change the dependence on input order, "None of these" does not hold.
So, among the offered choices, the technique that improves quick sort's behaviour on its worst inputs — by making the EXPECTED time O(n log n) regardless of input order — is randomization.