Abstract
- A algorithm for Finding the kth Smallest Element within an unsorted array. It is closely related to Quick Sort, sharing the same idea but with a key difference: Quick Select only focuses on one side of the partitioned array after each iteration
Quick Select's average time complexity is O(n)
In the ideal case, after each iteration, Quick Select is able to remove half of the elements in the current array. Thus, the total number of elements it needs to process throughout the algorithm is , which converges to based on the geometric series rule.
This is why picking a good pivot point is critical. If the chosen pivot consistently falls at the middle point of the sorted array, we can remove half of the elements after each iteration.
Why Quick Select's worst case time complexity is O(n^2)?
The worst-case scenario occurs when you consistently choose the smallest or largest element as the pivot. This leads to unbalanced partitions, where one subarray contains nearly all the elements while the other has very few. In this scenario, you end up making recursive calls, each taking time for partitioning, resulting in a total time complexity of . Quicksort also suffers from the same issue.
Choosing a pivot randomly significantly reduces the probability of consistently picking bad pivots. However, if the array contains mostly the same value, Quick Select’s time complexity will always be , as each iteration will invariably result in an unbalanced partition.
Quick Select Implementation
- The implementation below is using Java