Quicksort Choosing the pivot
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to Quicksort
Quicksort is an efficient, in-place sorting algorithm that follows the divide-and-conquer paradigm. In practice, it is one of the fastest sorting algorithms, making it a popular choice for a variety of applications. The main concept of Quicksort is to partition the array into sub-arrays around a pivot element, then recursively sort the sub-arrays.
Choosing the Pivot
The efficiency of Quicksort significantly depends on the method used to choose the pivot. The choice of pivot can lead to different runtimes, from the best-case to the worst-case complexities. Thus, selecting a good pivot is crucial for optimizing Quicksort.
Common Pivot Selection Strategies
- First Element:
- The simplest strategy is to choose the first element of the array as the pivot. This can lead to poor performance if the array is already or nearly sorted, resulting in the worst-case time complexity.
- Last Element:
- Similar to the first element, the last element can also serve as a pivot. However, it suffers from the same potential drawbacks as choosing the first element.
- Random Element:
- Choosing a random element as a pivot helps in achieving a more balanced partition on average. This method reduces the likelihood of degenerating into the worst-case scenario and typically results in good average performance.
- Median of Three:
- This strategy involves choosing the median of the first, middle, and last elements of the array as the pivot. This approach tends to provide better performance by creating more balanced partitions, especially for arrays with certain fixed patterns.
- Median of Medians:
- A more sophisticated method, this strategy aims to find a more accurate median and thus improve partitioning. This involves dividing the array into groups, finding the median of each group, and then choosing the median of these medians.
Example Demonstrations
To visualize how pivot selection affects Quicksort, consider an unordered array:
- First Element as Pivot:
- Pivot:
3 - Partitions around
3:[1, 2, 1] | 3 | [6, 8, 10]
- Random Element as Pivot (e.g., Randomly chosen
8):- Pivot:
8 - Partitions around
8:[3, 6, 1, 2, 1] | 8 | [10]
- Median of Three (Consider first
3, middle10, last1):- Median:
3 - Partitions around
3:[1, 2, 1] | 3 | [6, 8, 10]
Impact of Pivot Selection on Performance
The choice of pivot affects the balance of the partitions, which in turn influences the depth of recursion and the number of operations needed to sort sub-arrays. A balanced partition results in fewer recursive steps and closer to the optimal runtime of , which is the theoretical lower bound for comparison-based sorting algorithms.
Comparative Summary
| Strategy | Pros | Cons |
| First Element | Simple and requires no extra space | Poor for already sorted arrays |
| Last Element | Easy implementation | Suffers same issues as the first element |
| Random Element | Reduces risk of worst-case performance | Still random; unpredictable performance |
| Median of Three | Generally leads to better balance | Slightly more complex to implement |
| Median of Medians | Produces more optimal pivots | Computationally expensive |
Conclusion
Selecting a pivot method for Quicksort plays a critical role in its efficiency. While the median of medians provides the most balanced approach, it is often more computationally intensive. Practically, choosing a random pivot or using the median of three technique offers a good trade-off between simplicity and performance. Understanding the implications of each strategy can help tailor Quicksort to specific problem instances, ensuring optimal performance for a wide variety of input arrays.
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.