Quick Select
Quick select often comes up when the question asks for the first/last K elements or the Kth largest/smallest element
Last updated
Quick select often comes up when the question asks for the first/last K elements or the Kth largest/smallest element
Last updated
Best/Average: can guaranteed by shuffling the array
Worst: if sorted
Arrange largest elements in front by inverting the pivot comparison. This is a common problem type that tests if you know exactly how the partitioning algorithm works.
Pick a pivot, partition elements smaller than the pivot to the left, and elements larger to the right of the pivot
idx-1
is the pivot index after performing the partitioning the array. Think of idx
as the position for the next element that is smaller than the pivot value
Easier to implement