Quick sort Worst case
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Quick sort is a popular and efficient sorting algorithm that follows the divide-and-conquer paradigm. It works by selecting a 'pivot' element from an array and partitioning the other elements into two sub-arrays: one with elements less than the pivot and one with elements greater than the pivot. The process is then recursively applied to the sub-arrays. Despite being efficient in practice, quick sort can exhibit a worst-case performance that is important to understand. In this article, we'll delve into the technical intricacies of quick sort's worst-case scenario and examine its implications.
Technical Explanation
Pivot Selection
The performance of quick sort heavily depends on the choice of the pivot. Ideally, the pivot should divide the array into two approximately equal halves, leading to balanced partitions and a time complexity of . However, if the pivot is poorly chosen, the partitions can be unbalanced, leading to a worst-case scenario.
Worst-case Scenario
The worst-case time complexity of quick sort is . This occurs when the pivot selection consistently results in the most unbalanced partitions possible, such as:
- Sorted Array: If the array is already sorted (or reverse-sorted) and the first or last element is always chosen as the pivot, quick sort will degrade to its worst-case time complexity.
- Choosing Extremes: Similarly, choosing either the maximum or minimum element as the pivot repeatedly will lead to the worst-case scenario.
Detailed Example
Consider the sorted array `[1, 2, 3, 4, 5]` and using the first element as the pivot:
- Pivot = 1; Partition results in [], [2, 3, 4, 5]
- Pivot = 2; Partition results in [], [3, 4, 5]
- Pivot = 3; Partition results in [], [4, 5]
- Pivot = 4; Partition results in [], [5]
- Pivot = 5; Partition results in [], []
In this example, each partition operation processes one less element, creating a call stack of depth `n`, thus leading to a time complexity of .
Improvements to Avoid the Worst-case
- Randomized Quick Sort: By picking a random element as the pivot, the likelihood of hitting the worst-case scenario on every call greatly diminishes, improving the expected performance to .
- Median-of-Three Pivot Selection: In this approach, the pivot is chosen as the median of the first, middle, and last elements of the array. This is a heuristic that can often prevent unbalanced partitions and improve performance in practical situations.
- Hybrid Approaches: Algorithms like introsort start with quick sort but switch to more stable algorithms such as heap sort when the recursion depth exceeds a certain threshold.
Quick Sort Worst-case Time Complexity Table
| Aspect | Worst-case Scenario | Improved Techniques |
| Time Complexity | (expected with improvements) | |
| Partitioning | Highly unbalanced due to poor pivot choice | Balance improved via randomized or median-of-three pivot selection |
| Common Causes | Sorted or reverse-sorted arrays | Random pivot selection, hybrid algorithms |
| Improvement Techniques | Simple pivot choice (e.g., first/last) | Randomized Quick Sort, Median-of-Three, Introsort |
| Algorithm Behavior | Degrades to iterating over each element | Close to divide-and-conquer efficiency with balanced partitions |
Conclusion
Understanding the worst-case scenario of quick sort is crucial for both theoretical and practical applications. While quick sort is efficient in practice and widely used due to its average-case time complexity, its performance can degrade significantly with poor pivot selection. By employing strategies such as randomized pivot selection, median-of-three methods, or hybrid algorithms, the impact of the worst-case scenario can be mitigated, ensuring that quick sort remains a robust choice for sorting tasks.
Related reading
- Quickest way to find missing number in an array of numbers
- Quickly checking if set is superset of stored sets
- Quickselect Algorithm - Simplified Explanation
- QuickSelect Algorithm Understanding
- Quickest way to delete enormous MySQL table
- QuickSelect with Hoare partition scheme
- Quicksort - Hoare's partitioning with duplicate values
- Quicksort - which sub-part should be sorted first?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.