Skip to content

Added RandomizedClosestPair code and test #6224

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jivi-this-side
Copy link

@Jivi-this-side Jivi-this-side commented Apr 17, 2025

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Randomized Closest Pair of Points : (As per issue #6219 added this algo to repo)

The Closest Pair of Points problem finds the minimum Euclidean distance between two points in a 2D plane.

Randomized Algorithm Approach:

  • Randomly shuffle the input points.

  • Use a sweep-line + grid bucketing strategy to maintain only nearby points for efficient lookup.

  • Continuously update the closest pair distance while sweeping from left to right.

⚡ Time Complexity

  • Expected Time: O(n)

  • Worst-case Time: O(n log n)

Use Cases

  • Geospatial clustering

  • Nearest-neighbor search

  • Computational geometry in games, simulations, and robotics

Thank you!

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 91.11111% with 4 lines in your changes missing coverage. Please review.

Project coverage is 73.85%. Comparing base (ad5e496) to head (f111af6).

Files with missing lines Patch % Lines
...healgorithms/randomized/RandomizedClosestPair.java 91.11% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6224      +/-   ##
============================================
+ Coverage     73.81%   73.85%   +0.03%     
- Complexity     5311     5325      +14     
============================================
  Files           673      674       +1     
  Lines         18376    18421      +45     
  Branches       3553     3562       +9     
============================================
+ Hits          13565    13604      +39     
- Misses         4264     4267       +3     
- Partials        547      550       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@DenizAltunkapan DenizAltunkapan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!
Please fix the pipeline (GitHub Actions) and adjust the requested changes so that the Checkstyle rules will pass. The rest looks pretty good to me 🙂

}

public static class Point {
public final double x, y;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each variable declaration must be in its own statement. Please split these two declarations.

Comment on lines +59 to +63
Point[] Qx = Arrays.copyOfRange(px, 0, mid);
Point[] Rx = Arrays.copyOfRange(px, mid, n);

List<Point> Qy = new ArrayList<>();
List<Point> Ry = new ArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please write qx instead of Qx because this is not camelCase.

Comment on lines +65 to +66
if (p.x <= midPoint.x) Qy.add(p);
else Ry.add(p);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'if' construct must use '{}'s (see checktyle)

private static double distance(Point p1, Point p2) {
return Math.hypot(p1.x - p2.x, p1.y - p2.y);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line has trailing spaces according to checkstyle, please remove this

double result = RandomizedClosestPair.findClosestPairDistance(points);
assertEquals(5.0, result, 0.00001);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line has trailing spaces according to checkstyle, please remove this

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.

3 participants