The OpenD Programming Language

partitionK

Partitions the input data according to compFun, such that position k contains the kth largest/smallest element according to compFun. For all elements e with indices < k, !compFun(datak, e) is guaranteed to be true. For all elements e with indices > k, !compFun(e, datak) is guaranteed to be true. For example, if compFun is "a < b", all elements with indices < k will be <= datak, and all elements with indices larger than k will be >= k. Reorders any additional input arrays in lockstep.

partitionK
(
alias compFun = "a < b"
T...
)
(,
ptrdiff_t k
)

Return Value

Type: ElementType!(T[0])

The kth element of the array.

Examples

auto foo = [3, 1, 5, 4, 2].dup;
auto secondSmallest = partitionK(foo, 1);
assert(secondSmallest == 2);
foreach(elem; foo[0..1]) {
    assert(elem <= foo[1]);
}
foreach(elem; foo[2..$]) {
    assert(elem >= foo[1]);
}

Meta