Skip to content

feat(searching): add k closest points to origin (heap and quickselect)#591

Merged
0xDevNinja merged 1 commit intomainfrom
feat/searching/k-closest-points
May 7, 2026
Merged

feat(searching): add k closest points to origin (heap and quickselect)#591
0xDevNinja merged 1 commit intomainfrom
feat/searching/k-closest-points

Conversation

@0xDevNinja
Copy link
Copy Markdown
Owner

Summary

  • Implement two algorithms for finding the k closest 2D points to the origin in src/searching/k_closest_points.rs:
    • k_closest_heap — bounded max-heap of size k keyed 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 by points.len(); O(n) average / O(n^2) worst time, O(n) extra space.
  • Both routines compare squared distances (no sqrt), share NaN-tolerant ordering, and document that returned-vector order is unspecified with ties broken by encounter order.
  • Edge cases handled: k == 0 returns empty, k >= n returns all, empty input returns empty.

Closes #381

Test plan

  • cargo fmt
  • cargo build
  • cargo clippy --all-targets -- -D warnings
  • cargo test (2668 unit + 51 doctests pass; 10 new unit tests cover k=0, empty input, k > n, k == n, simple known example, single point, cross-impl agreement on small inputs, count invariant across all k, brute-force distance multiset check, and tied-distance behavior)

@0xDevNinja 0xDevNinja force-pushed the feat/searching/k-closest-points branch from 44eb983 to 49ca7a1 Compare May 7, 2026 10:19
@0xDevNinja 0xDevNinja merged commit c92b476 into main May 7, 2026
@0xDevNinja 0xDevNinja deleted the feat/searching/k-closest-points branch May 7, 2026 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add k closest points to origin (heap and quickselect variants)

1 participant