Quick select with repeat values
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 select is an efficient selection algorithm to find the smallest (or largest) element in an unordered list. It is based on the same partitioning logic that underlies the Quick sort algorithm. While Quick select is quite efficient for finding a single element, its behavior and performance can be influenced by various factors, such as the presence of duplicate values. This article delves into the Quick select algorithm, especially in the context of repeated values, providing detailed technical explanations and examples.
Introduction to Quick Select
Quick select works by selecting a "pivot" element from the array and partitioning the array into two sub-arrays: elements less than the pivot and elements greater than or equal to the pivot. The pivot's final position is identified during the process. If this position matches , then the pivot is the smallest element. If it is greater than , Quick select recurs on the left sub-array; otherwise, it continues with the right sub-array.
Handling Repeated Values
When elements are repeated, the partitioning step can become more complex, especially if the pivot value is one of these repeats. A naive implementation may lead to an inefficient selection as partitioning might not significantly reduce the size of the subarray to be further explored.
Example with Repeated Values
Consider the array [3, 1, 2, 2, 4, 3]
and finding its smallest element. Let's perform Quick select:
- Initial Array:
[3, 1, 2, 2, 4, 3]• Select pivot:3• Partition arrays:[2, 1, 2] | 3 | [4, 3]• Since position of pivot3 > 3, andk = 3, we need to search in[2, 1, 2]. - Sub-array:
[2, 1, 2]• Select pivot:2. • Partition arrays:[1] | 2 | [2]• The pivot2is at 2nd position (0-indexed). • We need to find in[2](as the count of distinct smaller elements is less than 3). - Final element found:
2
Quick select, even with this basic approach, provides the correct result. However, efficiency may be improved using techniques like randomization of pivot selection.
Algorithm Efficiency and Complexity
The average time complexity of Quick select is due to the partitioning process, whereas its worst-case complexity is , particularly significant when dealing with a poor pivot choice. Efficiency concerns arise prominently with repeated values.
Improved Selection Techniques
• Randomized Quick Select: By choosing a random pivot, Quick select can reduce the example of worst-case time complexity caused by poor pivot choices, especially in arrays with repeated elements.
• Median-of-medians: A deterministic, albeit more complex, approach to improving pivot selection involves finding a good pivot by calculating the median of medians, ensuring time complexity consistently.
Here’s a summary of the Quick select process with repeated values:
| Aspect | Explanation |
| Algorithm | Selection algorithm based on partitioning logic, akin to Quick sort. |
| Average Time Complexity | , efficient for large arrays when used optimally. |
| Worst-case Time Complexity | , particularly if partitioning inefficiently splits the array. |
| Handling Repeated Values | May increase the frequency of worst-case scenario unless pivot selection is improved. |
| Applications | Useful for median or order statistics calculations. |
| Improvements | Randomization, median-of-medians enhance efficiency in presence of duplicates. |
Conclusion
Quick select is a powerful tool for finding order statistics efficiently. While unoptimized versions may struggle with repeated values, clever pivot strategies can mitigate this challenge, allowing Quick select to harness its full potential. As a cornerstone in algorithms involving selection problems, understanding its workings and enhancements is crucial for efficient data handling.
Related reading
- quick sort algorithm improvement if more duplicate keys
- Quick Sort Vs Merge Sort
- Quick sort Worst case
- Quickest way to find missing number in an array of numbers
- Quickly checking if set is superset of stored sets
- Quickselect Algorithm - Simplified Explanation
- Quickest way to delete enormous MySQL table
- QuickSelect with Hoare partition scheme

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.