Sorting algorithms
Quick select
Data structures
Computer science
Algorithm optimization

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.

Practice algorithms

Quick select is an efficient selection algorithm to find the kthk^{th} 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 kk, then the pivot is the kthk^{th} smallest element. If it is greater than kk, 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 3rd3^{rd} smallest element. Let's perform Quick select:

  1. Initial Array: [3, 1, 2, 2, 4, 3]
    • Select pivot: 3
    • Partition arrays: [2, 1, 2] | 3 | [4, 3]
    • Since position of pivot 3 > 3 , and k = 3 , we need to search in [2, 1, 2] .
  2. Sub-array: [2, 1, 2]
    • Select pivot: 2 . • Partition arrays: [1] | 2 | [2]
    • The pivot 2 is at 2nd position (0-indexed). • We need to find in [2] (as the count of distinct smaller elements is less than 3).
  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 O(n)O(n) due to the partitioning process, whereas its worst-case complexity is O(n2)O(n^2), 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 O(n)O(n) time complexity consistently.

Here’s a summary of the Quick select process with repeated values:

AspectExplanation
AlgorithmSelection algorithm based on partitioning logic, akin to Quick sort.
Average Time ComplexityO(n)O(n), efficient for large arrays when used optimally.
Worst-case Time ComplexityO(n2)O(n^2), particularly if partitioning inefficiently splits the array.
Handling Repeated ValuesMay increase the frequency of worst-case scenario unless pivot selection is improved.
ApplicationsUseful for median or order statistics calculations.
ImprovementsRandomization, 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
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
Track 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.

Practice algorithms

All Rights Reserved.