feat(searching): add k closest points to origin (heap and quickselect)#591
Merged
0xDevNinja merged 1 commit intomainfrom May 7, 2026
Merged
feat(searching): add k closest points to origin (heap and quickselect)#5910xDevNinja merged 1 commit intomainfrom
0xDevNinja merged 1 commit intomainfrom
Conversation
44eb983 to
49ca7a1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kclosest 2D points to the origin insrc/searching/k_closest_points.rs:k_closest_heap— bounded max-heap of sizekkeyed by squared distance,O(n log k)time,O(k)extra space.k_closest_quickselect— Hoare-style in-place partition with a deterministic xorshift64* pivot seeded bypoints.len();O(n)average /O(n^2)worst time,O(n)extra space.sqrt), share NaN-tolerant ordering, and document that returned-vector order is unspecified with ties broken by encounter order.k == 0returns empty,k >= nreturns all, empty input returns empty.Closes #381
Test plan
cargo fmtcargo buildcargo clippy --all-targets -- -D warningscargo test(2668 unit + 51 doctests pass; 10 new unit tests coverk=0, empty input,k > n,k == n, simple known example, single point, cross-impl agreement on small inputs, count invariant across allk, brute-force distance multiset check, and tied-distance behavior)