collections/quickselect_utils library

Quickselect: the k-th smallest/largest element without fully sorting. Roadmap #52.

Functions

nthLargest<T>(Iterable<T> items, int k, Comparator<T> compare) → T?
Returns the k-th largest element (0-based: k = 0 is the maximum), or null if k is out of range. Equivalent to nthSmallest from the other end. Audited: 2026-06-12 11:26 EDT
nthSmallest<T>(Iterable<T> items, int k, Comparator<T> compare) → T?
Returns the element that would sit at 0-based index k if items were sorted by compare, without paying for a full sort.