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.

Try Partition Select in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions