Quicksort complexity when all the elements are same?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding Quicksort Complexity When All Elements Are the Same
Quicksort is a popular sorting algorithm that employs a divide-and-conquer strategy to sort elements efficiently. It is renowned for its average-case time complexity of . However, like many algorithms, its performance can deteriorate under specific circumstances, leading to worst-case scenarios. One such situation occurs when all elements in the input array are the same. This article provides an in-depth analysis of Quicksort's complexity when faced with this peculiar but interesting input distribution.
Quicksort Basics
Quicksort works by selecting a "pivot" element from the array and partitioning the other elements into two subsets: those less than the pivot and those greater than the pivot. It then recursively sorts the subsets. The choice of the pivot and the partitioning scheme significantly influence the algorithm's performance.
Analyzing Complexity with Uniform Elements
When all elements in the array are identical, every element is equal to any potential pivot. In such a scenario, the partitioning process does not reduce the problem size effectively, which adversely impacts the algorithm’s complexity. Let's delve into the specifics:
Partitioning with Uniform Values
- Pivot Selection & Partitioning:
- Choose any element as the pivot.
- All elements are equal to the pivot, so they all fall into one partition group (equally grouped with the pivot), while the other partition is effectively empty.
- No reduction in problem size after each partition.
- Recursive Calls:
- The partitioning leads to one empty subset and one 'n-1' sized subset for an array of size 'n'.
- This results in recursive calls on an size problem each time, mimicking a linked list type structure.
- Complexity Estimation:
- T(n) = T(n-1) + O(n)
- Leads to a recurrence relation similar to that of linear search.
- Solving this gives: .
The complexity becomes , the worst case for Quicksort, as the algorithm performs unnecessary comparisons and swaps due to ineffective partitioning.
Example
Consider an array `[5, 5, 5, 5, 5]`:
- Choose `5` as the pivot.
- After partitioning, the array remains the same since all values are equivalent.
- Proceed with the recursive step on a reduced problem size of `[5, 5, 5, 5]`, repeating until size becomes zero.
This example clearly highlights the redundant operations Quicksort undertakes when faced with uniform data, emphasizing the ineffective problem size reduction.
Mitigating the Worst-Case Scenario
Although Quicksort is inherently prone to poor performance with uniform inputs, several strategies can mitigate this effect:
- Randomized Quicksort:
- Select pivots randomly to reduce deterministic behavior, helping average out the time complexity across various inputs.
- Three-Way Partitioning:
- Integrate a three-way partitioning technique to handle duplicate keys more efficiently by dividing the array into three parts:
- Elements less than the pivot
- Elements equal to the pivot
- Elements greater than the pivot
This strategic approach reduces unnecessary recursive calls on duplicate values, significantly improving performance to expected for arrays with many duplicates.
Comparison Table
| Aspect | Standard Quicksort | Quicksort with Uniform Elements |
| Pivot Selection | Random/Fixed strategies | Leads to ineffective partitioning |
| Partitioning | Divides into less and less than or equal to pivot | All elements equal pivot |
| Recursive Calls | Logarithmic tree height on average | Linear tree height |
| Complexity | Average: Worst: | Worst: |
| Optimization | Randomization Three-way partitioning | Significant improvement with three-way partitioning |
Conclusion
The Achilles' heel of Quicksort is indeed its performance when all array elements are identical. Although Quicksort exhibits suboptimal complexity under this condition, recognizing such pitfalls allows developers to tailor its robustness for specific data scenarios. Employing techniques like randomized pivot selection and three-way partitioning can effectively transform this "Achilles' heel" into an opportunity for reliability, ensuring efficient performance across a diverse range of datasets. As with many computer science problems, understanding the nuances of specific inputs can lead to more elegant and robust algorithms in practice.
Related reading

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.