Algorithm. Quickselect [00QD]
\mathsf {require} 0 \leq k < |A|
\mathsf {Quickselect}(A,k) =
\mathsf {if} |A| = 1:
\mathsf {return} A[0]
\mathsf {else}:
A_1, A_2 \gets \mathsf {Partition}(A)
\mathsf {if} k < |A_1|:
\mathsf {return} \mathsf {Quickselect}(A_1, k)
\mathsf {else}:
\mathsf {return} \mathsf {Quickselect}(A_2, k - |A_1|)
\mathsf {Partition}(A) picks a random pivot value in A, and returns two lists: one containing all the values in A which are less than the pivot, and one containing all values greater than or equal to the pivot.
Note that in practice, one would implement this so it all works in-place on the array A, instead of generating new arrays A_1 and A_2, e.g. by passing along extra \mathit {hi} and \mathit {lo} parameters to specify which part of the array to focus on.