算法设计技巧与分析p123算法6.7
这是一个相当有难度的问题,给出n个点在O(nlogn)的时间复杂度内求出距离最近的两点间的距离,具体分析可以参考书本,用到了分治的思想,另外,注意每个点最多只需要和临近的7个点相比较就能求出最近距离,这是本算法的一个关键之处。
/**
*
* @author yyd
* 欧几里得算法得出平面上举例最近的两个点
*
*/
class Point {
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public class CLOSESTPAIR {
public static double closet(int low,int high,Point[] S,Point[] Y){
if(high-low<=2){
if(high-low==1){
return dist(S[low],S[high]);
}
else{ //此时high-low=2
int temp=low+1;
double d1=dist(S[low],S[temp]);
double d2=dist(S[temp],S[high]);
double d3=dist(S[low],S[high]);
double min