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.

ElementType!(T[0])
partitionK
(
alias compFun = "a < b"
T...
)
(
,
ptrdiff_t k
)
in { assert (data.length > 0); size_t len = data[0].length; foreach (array; data[1 .. $]) { assert (array.length == len); } }

Return Value

Type: ElementType!(T[0])

The kth element of the array.

Examples

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

Meta