-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: slices: add functions MinNFunc and MaxNFunc #67376
Comments
this sounds very specialized |
I think this is the wrong proposal. Instead container/heap should be made generic, and you can just pop the first N items off a heap. See #47632. |
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).
If you mean that 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. |
I was just now going to open a new proposal, and ran into this issue. Let me start by chiming in here. What you're describing here sounds like a selection algorithm which has average case O(n), like quick select. I've recently implemented pdqselect and published it open source: https://github.com/tsenart/pdqselect It's based on Go's pdqsort implementation, and in fact all the zsort*.go files are copied from the Go tree. The algorithm is an adaptation of pdqsort to be a selection algorithm like quick select. It exists similarly in the Rust and C++ ecosystems:
For inclusion in the stdlib, we could have something like this (follow my module's current design, more or less):
With each of these ensuring only that after they're called the elements at Not sure if the Go team is open to consider including something like this in the stdlib, but worth a discussion, given there's precedent in other languages, general utility and most of the code is already written — there's not a lot of new code in my module, mostly just |
Also, cc @zhangyunhao116 and @eliben, would like your input on the above, given your pdqsort work recently. |
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: