# partitionSelect() Function & Examples

Use the partitionSelect() matrix function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## partitionSelect

#### partitionSelect(x, k)

#### partitionSelect(x, k, compare)

Try it yourself:

```calculio
partitionSelect([5, 10, 1], 2)
partitionSelect(["C", "B", "A", "D"], 1, compareText)
arr = [5, 2, 1]
partitionSelect(arr, 0) # returns 1, arr is now: [1, 2, 5]
arr
partitionSelect(arr, 1, 'desc') # returns 2, arr is now: [5, 2, 1]
arr
```

[sort](https://calcul.io/function/sort/index.md)

---

## 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()](https://calcul.io/function/sort/index.md) 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()](https://calcul.io/function/mean/index.md)-style reporting, or preparing robust summaries with [quantileSeq()](https://calcul.io/function/quantileSeq/index.md). Use [compare()](https://calcul.io/function/compare/index.md) for an explicit ordering rule and [size()](https://calcul.io/function/size/index.md) 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](https://calcul.io/functions/index.md)
