quicksort
randomized algorithms
median-of-three
algorithm performance
sorting algorithms

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.

Practice algorithms

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 O(n2)O(n^2) to an expected O(nlog⁡n)O(n \log n), even in the case of already sorted input arrays. The main steps involved in randomized quicksort include:

  1. Random Pivot Selection: Pick a random element from the array as the pivot.
  2. Partitioning: Rearrange the array into two parts: elements less than the pivot and elements greater than the pivot.
  3. 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:

  1. Choose Three Random Elements: Randomly select three elements from the array.
  2. Determine the Median: Find the median of the three chosen elements.
  3. Use Median as Pivot: Use this median as the pivot for partitioning.
  4. 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 A=[9,7,5,11,12,2,14,3,10,6]A = [9, 7, 5, 11, 12, 2, 14, 3, 10, 6].

  • 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

CharacteristicsRandomized QuicksortMedian-of-Three Quicksort
Pivot SelectionSingle random elementMedian of three elements
Expected ComplexityO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)
Worst Case ComplexityO(n2)O(n^2)O(n2)O(n^2)
Overhead CostLowHigher
Performance on Larger ArraysGoodBetter for balanced data
Sensitivity to Input PatternsHigherLower
Cache UtilizationModerateHigher

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.


Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms