- Asked 2/14
- 2069 Poush · 3 marks
- 2068 Magh · 2 marks
Efficiency of quick sort
Short answer
Quick sort divides using a pivot. Best/average case T(n)=2T(n/2)+cn solves to O(n log n). The worst case with an already sorted array is O(n²). Random or median-of-three pivot makes O(n log n) typical.
Show that efficiency of quick sort is O(n log(n)). (Calculate the efficiency of quick sort.)
Quick sort picks a pivot, partitions the array so that smaller elements are on its left and larger ones on its right, then sorts both parts recursively. Partitioning n elements takes n − 1 comparisons, i.e. O(n).
Let T(n) be the time to sort n elements and let the pivot end at position k.
T(n) = T(k) + T(n − k − 1) + cn
Best case (pivot splits the array in the middle)
T(n) = 2T(n/2) + cn
Expanding: T(n) = 2[2T(n/4) + cn/2] + cn = 4T(n/4) + 2cn = ... = 2ʲ T(n/2ʲ) + j·cn.
The recursion stops when n/2ʲ = 1, i.e. j = log₂ n, so T(n) = n·T(1) + cn·log₂ n = O(n log n).
Another view: there are log₂ n levels of recursion and each level does about n comparisons in total, giving n log₂ n.
Average case
For random data the pivot lands at every position with equal probability: T(n) = (1/n) Σₖ [T(k) + T(n−k−1)] + cn, whose solution is about 1.39·n·log₂ n, still O(n log n).
Worst case
The array is already sorted (first element as pivot): one part is empty and the other has n − 1 elements, so T(n) = T(n−1) + cn = c(n + (n−1) + ... + 1) = cn(n+1)/2 = O(n²).
| Case | Complexity |
|---|---|
| Best | O(n log n) |
| Average | O(n log n) |
| Worst | O(n²) |
Choosing the median-of-three or a random pivot makes the worst case very unlikely. Extra space is O(log n) for the recursion stack.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Efficiency of straight selection sort · Data Structure and Algorithm, chapter 7
- Jet on flat plate, force and efficiency · Fluid Mechanics, chapter 7
- Trickle charging and Ah/Wh efficiency (4 A, 12 h, 1.2 V) · Industrial Power Distribution and Illumination, chapter 9 · asked 3×
- Trickle charging and Ah/Wh efficiency (2.2 V, 2.44 V) · Industrial Power Distribution and Illumination, chapter 9 · asked 3×
- Battery charger bridge 15 V: currents, power, efficiency · Power Electronics, chapter 2 · asked 3×