Loading...
Divide-and-conquer refinement of the brute force O(n^2) pairwise check. Spatial ordering + geometric constraints bring time to O(n log n).
Given n points in 2D, find the pair with minimum Euclidean distance. A brute force O(n^2) approach compares all pairs; divide and conquer reduces this to O(n log n) by pruning comparisons via geometric constraints in a narrow vertical strip around the splitting line.
Packing argument: Inside a delta × 2delta rectangle you cannot pack more than 8 points mutually farther than delta apart. After y-sorting, only the next 7 neighbors can beat delta; further points exceed delta vertically.
1function closestPair(points):2 sort points by x3 return recursive(points)4function recursive(P):5 if |P| <= 3: return bruteForce(P)6 mid = |P|/2; L = P[0:mid]; R = P[mid:]7 d = min(recursive(L), recursive(R))8 strip = points within d of mid line9 return min(d, stripClosest(strip, d))