partitionSelect

partitionSelect(x, k)

partitionSelect(x, k, compare)

Try it yourself:

See also:

partitionSelect(): find an order statistic efficiently

partitionSelect(array, k) partitions values to select the kth ordered item without fully sorting the whole array. It is useful for medians, percentiles, and threshold selection on large lists.

Selection versus sorting

A full sort() orders every item. Partition selection instead ensures the chosen rank is in its correct position, which can require less work when only one rank matters.

Applications

Use it when calculating a median before mean()-style reporting, or preparing robust summaries with quantileSeq(). Use compare() for an explicit ordering rule and size() to validate available ranks.

Caveat

Check whether indexes are zero- or one-based and whether the operation mutates the input collection before reusing it.

All functions