QuickSelect Algorithm
QuickSelect Algorithm
QuickSelect is a variant of the QuickSort algorithm, but instead of sorting the entire array, it
only partially sorts the array until it finds the k-th smallest or largest element.
The key insight of QuickSelect is that if we pick a pivot (as in QuickSort), we can use the
partitioning process to narrow down where the k-th element is. We only need to recursively
focus on the part of the array that contains the k-th element.
The expected time complexity is O(n) for average cases (since each partition step reduces the
problem size by roughly half), but it can degrade to O(n^2) in the worst case if the pivot is
poorly chosen (e.g., if it always picks the smallest or largest element).