更快地找到最接近点对 [英] Faster way to find the closest pair of points
问题描述
我想比较数组彼此的所有元素。我可以嵌套循环做到这一点,但它是一个非常低效的算法,我可以告诉它不是正确的方式去做。下面是我在做什么现在。
I'm trying to compare all elements of an array to each other. I can do it with nested loops but it's a very inefficient algorithm and I can tell it's not the right way to do it. Here's what I'm doing right now.
市盈率答案在下面,我改变了这个code,我在不断扩大的问题。
PER answers below I've changed this code and I'm expanding on the question.
// Point from java.awt.Point;
private static void findShortestDistance(Point[] pt) {
ArrayList<Double> distance = new ArrayList<Double>(1000);
for(int i=0; i<pt.length; i++) {
for(int j=i+1; j<pt.length; j++) {
double tmp = pt[i].distance(pt[j]);
distance.add(tmp);
}
}
double min = distance.get(0);
for(Double d : distance) {
if(d < min) { min = d; }
}
}
有充分code的方法,我有这么远。我试图找到给定数组中两点之间最短的距离。
There's the full code for the method I have so far. I'm trying to find the shortest distance between two points in the given array.
推荐答案
有一个看看维基百科。 http://en.wikipedia.org/wiki/Closest_pair_of_points
Have a look at wikipedia. http://en.wikipedia.org/wiki/Closest_pair_of_points
和这个问题似乎是相同 <一href="http://stackoverflow.com/questions/1602164/shortest-distance-between-points-algorithm">Shortest点算法之间的距离
And this question seems to be the same as Shortest distance between points algorithm
这篇关于更快地找到最接近点对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!