topN

fnauto topN(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r, size_t nth) if (isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range && hasAssignableElements!Range)

Reorders the range r using swap such that r[nth] refers to the element that would fall there if the range were fully sorted.

It is akin to Quickselect, and partitions r such that all elements e1 from r[0] to r[nth] satisfy !less(r[nth], e1), and all elements e2 from r[nth] to r[r.length] satisfy !less(e2, r[nth]). Effectively, it finds the nth + 1 smallest (according to less) elements in r. Performs an expected r.length (if unstable) or r.length * log(r.length) (if stable) evaluations of less and swap.

If n >= r.length, the algorithm has no effect and returns r[0 .. r.length].

Parameters

lessThe predicate to sort by.
ssThe swapping strategy to use.
rThe random-access range to reorder.
nthThe index of the element that should be in sorted position after the function is done.

Returns

a slice from r[0] to r[nth], excluding r[nth] itself.

See Also

topNIndex,

BUGS:

Stable topN has not been implemented yet.

fnauto topN(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range1, Range2)(Range1 r1, Range2 r2) if (isRandomAccessRange!(Range1) && hasLength!Range1 && isInputRange!Range2 && is(ElementType!Range1 == ElementType!Range2) && hasLvalueElements!Range1 && hasLvalueElements!Range2)

Stores the smallest elements of the two ranges in the left-hand range.

Parameters

lessThe predicate to sort by.
ssThe swapping strategy to use.
r1The first range.
r2The second range.