You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this sounds very specialized
does it have wide usage elsewhere?
I am not aware of it being included in the stdlib of any popular language, granted. However, it is a simple, well-defined operation that I often find useful and that is not completely trivial to implement efficiently, but that can reuse a lot of the machinery in pdqsort.
As an additional data point, if you perform a web search for "k largest elements", it appears to be a fairly popular operation, but perhaps not for the right reasons (it would seem it's a standard interview question, for some reason).
Instead container/heap should be made generic, and you can just pop the first N items off a heap. See #47632.
If you mean that MinNFunc can be implemented using a priority queue (and therefore using a binary heap) in O(n log k) time, then you're absolutely right. However, doing it efficiently is not completely trivial (the naive implementation copies every element, even those that end up being discarded.)
If you mean that I should be using a priority queue instead of computing the prioritary elements on demand, then I cannot do that: the priority changes dynamically (it depends on the network peers, don't ask), and there's no obvious way to determine when the priorities have changed sufficiently to justify rebuilding the priority queue.
I agree that it'd be nice to have a generic priority queue, but that's orthogonal to this proposal.
Proposal Details
I propose the addition of a function
which returns the
n
smallest elements ofx
in increasing order. Iflen(x) < n
, thenMinNFunc
returnsx
sorted.MinNFunc(n, x, cmp)
is equivalent tobut it avoids an allocation of size O(
len(x)
).For symmetry, there could be a function
MaxNFunc
which returns then
largest elements ofx
in decreasing order.MaxNFunc(n, x, cmp)
is equivalent toThe text was updated successfully, but these errors were encountered: