Does quicksort with randomized median-of-three do appreciably better than randomized quicksort?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Quicksort is one of the most widely used sorting algorithms due to its efficiency and simplicity. Despite being invented in 1960 by Tony Hoare, it continues to be a robust choice for many applications. Quicksort operates by selecting a pivot element, partitioning the array into elements less than and greater than the pivot, and then recursively sorting the subarrays. The efficiency of quicksort largely depends on the pivot selection strategy. Here, we explore whether using a randomized median-of-three pivot selection strategy provides a significant improvement over the traditional randomized quicksort.
Randomized Quicksort
Randomized quicksort enhances traditional quicksort by selecting a random pivot. This diminishes the worst-case time complexity probability from to an expected , even in the case of already sorted input arrays. The main steps involved in randomized quicksort include:
- Random Pivot Selection: Pick a random element from the array as the pivot.
- Partitioning: Rearrange the array into two parts: elements less than the pivot and elements greater than the pivot.
- Recursive Sorting: Recursively apply the same method to the left and right subarrays.
The performance is generally good, but further optimization is often sought in high-performance applications.
Median-of-Three Quicksort
The median-of-three quicksort refines the pivot selection process by choosing the median of three randomly selected elements as the pivot. This choice intends to better approximate the median of the entire array, leading to improved partitioning and thereby enhancing performance on average.
Steps for Median-of-Three Quicksort:
- Choose Three Random Elements: Randomly select three elements from the array.
- Determine the Median: Find the median of the three chosen elements.
- Use Median as Pivot: Use this median as the pivot for partitioning.
- Partition and Sort: Proceed with partitioning and recursive sorting as in the randomized quicksort.
Performance Analysis
To evaluate if a median-of-three strategy truly provides an appreciable improvement, consider these factors:
- Partition Evenness: A pivot nearer to the actual median yields more balanced partitions, reducing average depth of recursive calls.
- Cache Efficiency: Fewer and more evenly sized partitions can lead to better use of the CPU cache.
- Overhead: The computational cost of selecting the median of three is non-negligible, and its benefits depend on the input array characteristics.
Example Illustrations
Imagine sorting an array .
- Randomized Quicksort Example:
- Randomly select `9` as a pivot.
- Partition into `[7, 5, 2, 3, 6] | 9 | [11, 12, 14, 10]`.
- Recursive calls continue with two subarrays.
- Median-of-Three Quicksort Example:
- Choose three random elements, say `[9, 5, 11]`.
- Determine median, which is `9`.
- Similar partition to randomized, resulting in the same initial partitions as above, but potentially choosing different partitions in further recursive steps.
Comparisons in Performance
| Characteristics | Randomized Quicksort | Median-of-Three Quicksort |
| Pivot Selection | Single random element | Median of three elements |
| Expected Complexity | ||
| Worst Case Complexity | ||
| Overhead Cost | Low | Higher |
| Performance on Larger Arrays | Good | Better for balanced data |
| Sensitivity to Input Patterns | Higher | Lower |
| Cache Utilization | Moderate | Higher |
Conclusion
The effectiveness of quicksort heavily hinges on the choice of pivot. While the randomized quicksort offers simplicity and robustness, the median-of-three variant typically provides better partitioning, especially for large and nearly sorted datasets. While the overhead of computing the median can sometimes negate benefits in smaller datasets, when dealing with large data arrays or performance-critical applications, the improvement can be substantial.
Ultimately, the decision on which method to implement should consider the application requirements and data characteristics to determine if the benefits of median-of-three outweigh its additional computational overhead.
Related reading
- Does Raft send AppendEntries with logs right after election?
- Does stdcopy handle overlapping ranges?
- Does stdcopy_n work with overlapping ranges?
- Does stdsort implement Quicksort?
- Does Reactive programming consumes more resources than non-reactive?
- Does tensorflow or python have memory cleanup issues when using multiple models in loop?
- Does the dataset size influence a machine learning algorithm?
- Does the range-based 'for' loop deprecate many simple algorithms?

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.