Let S be a set of n points ((xi,yi), 1≤i≤n) in the plane, finding a pair of points p and q in S whose mutual distance is minimum.
The brute-force algorithm simply examines all the possible n(n−1)/2 distances and returns that pair with smallest separation.
The divide-and-conquer algorithm.
1)When n is small, use the brute-force algorithm to find the closest pair.
2) When n is large, divide the set S of points into equal parts A and B
Recursively find the closest pairs in A and B
Find the closest pair between point A and point B,
From the three closest pairs obtained above, find the pair with smallest separation.
(1) Divide
Sort the points in S by increasing x-coordinate. the point set S is divided about a vertical line L into two subsets Sl and Sr such that |Sl| =[|S|/2] and |Sr| = [|S|/2] . Let L be the vertical line passing by the x-coordinate of S[n/2]向下取整 . Thus, all points in Sl are on or to the left of L, and all points in Sr are on or to the right of L.
(2) Conquer
The minimum separations δl and δr of the two subsets Sl and Sr, respectively, are computed.
(3) Combine
The smallest separation δ’ between a point in Sl and a point in Sr is also computed. Finally, the desired solution is the minimum of δl, δr, and δ’.