chickadee » srfi-67 » kth-largest

(kth-largest compare k x[0] x[1] ...)procedure

The k-th largest element of values x[0] x[1] ...(one or more values) with respect to the compare procedure compare.

More precisely, (kth-largest compare k x[0] ... x[n-1]) returns the (modulo k n)-th element of the unique sequence obtained by stably sorting (x[0] ยทยทยท x[n-1]). (Recall that a sorting algorithm is stable if it does not permute items with equal key, i.e. equivalent w.r.t. compare).

The argument k is an exact integer, and n > 1. The order in which the values x[i] are compared is unspecified, but each value is compared at least once (even if there is just one value).

Note: The 0-th largest element is the minimum, the ( - 1)-st largest element is the maximum. The median is the (n - 1)/2-th largest element if n is odd, and the average of the (n/2 - 1)-st and n/2-th largest elements if n is even.