Function 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(data[k], e) is guaranteed to be true. For all elements e with indices > k, !compFun(e, data[k]) is guaranteed to be true. For example, if compFun is "a < b", all elements with indices < k will be <= data[k], and all elements with indices larger than k will be >= k. Reorders any additional input arrays in lockstep.

ElementType!(T[0]) partitionK(alias compFun, T...) (
  T data,
  ptrdiff_t k
);

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]);
}

Returns

The kth element of the array.