Find the value at a given rank without a full sort, handle tied observations and distinguish selection time from quantile estimation.
Quickselect
Quickselect finds the kth smallest value in an unsorted collection by partitioning around a pivot and continuing only in the group that contains the requested rank. It can extract a single loss threshold from a large scenario set without sorting every scenario. The returned value is the same order statistic that a full sort would put at position k.
Find the block that contains the rank
Count ranks from 1 in ascending order. Tied values each occupy a position. For a pivot v, split the active observations into groups strictly below, equal to and strictly above v. Let L count the observations below the pivot and E count those equal to it.
If k≤L, keep the lower group and search for the same rank.
If L<k≤L+E, return v. Every rank in the equal block has that value.
If k>L+E, keep the upper group and search for rank k−L−E within it.
Take eight toy losses of 9, 1, 8, 2, 7, 3, 6 and 6 USD. To find the fifth smallest, suppose the first pivot is 6. Three losses fall below it, two equal it and three exceed it. The two copies of 6 occupy ranks 4 and 5, so the answer is USD 6 after one partition.
Eight toy losses in USD split around pivot 6 into three smaller, two equal and three greater values. The two sixes occupy ranks 4 and 5, so the fifth-smallest loss is USD 6.
Partitioning locates the requested rank inside the equal block. The larger losses can stay in the order 9, 8, 7.
How much work can a pivot save?
Each partition scans the active group. If a pivot is drawn uniformly from the remaining observations at each step, it has a constant chance of cutting the active group by a constant fraction or finding the answer. The expected work across those shrinking groups is O(n), where n is the initial sample size. The expectation is over pivot choices for a fixed input.
With distinct values, a run can repeatedly pick an extreme pivot on the side away from the target and discard just one observation at a time. Scanning groups of sizes n,n−1,n−2,… gives Θ(n2) worst-case time. Deterministic median-of-medians selection spends extra work choosing pivots that guarantee O(n) time in the worst case. MIT, Order Statistics, Blum et al., Time Bounds for Selection.
The quantile rule comes first
To compute a sample quantile, first specify how the probability p maps to sample ranks. Inverting the empirical distribution uses rank ⌈np⌉ for 0<p≤1. At p=0.75, the eight losses give rank 6 and a threshold of USD 7. Type 7 interpolation uses fractional rank 1+(n−1)p=6.25. It goes a quarter of the way from the sixth-smallest loss, USD 7, to the seventh-smallest, USD 8, giving USD 7.25. Quickselect can retrieve the required observations for either rule. It changes the computation time while leaving the estimate and its sampling uncertainty unchanged. Hyndman and Fan, Sample Quantiles in Statistical Packages.
The SelfMadeQuant lesson Medians and Order Statistics: Quickselect traces the distinct-key case and shows how the target rank changes when the search moves to the upper group. The full lesson is for members.