1

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.

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; }
  }

}

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.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50

5 Answers5

3

Have a look at wikipedia. http://en.wikipedia.org/wiki/Closest_pair_of_points

And this question seems to be the same as Shortest distance between points algorithm

Community
  • 1
  • 1
Qian
  • 1,462
  • 1
  • 14
  • 25
  • Ah... Well it seems my entire method was wrong haha! Thanks for the info. I'll accept this answer as the correct one since it answers the question behind my original question. – CaldwellYSR Mar 05 '12 at 17:18
  • Sorry for not describing the detail of this algorithm. I don't remember it very well. – Qian Mar 05 '12 at 17:23
  • That's okay, I'd like to figure out the actual code if at all possible. I'm still learning this stuff. Thanks for the wiki link, that will help alot. – CaldwellYSR Mar 05 '12 at 17:27
0

Initializing j to i+1 will skip the redundant comparisons.

for(int i = 0; i < pt.length; i++) {
  for(int j = i + 1; j < pt.length; j++) {
    if(pt[i] != pt[j]) { /* do stuff */ }
  }
}

(BTW, changed the if-statement, assuming you meant to use i and j as indexes into an array.)

That's about the best you can get for the most basic case where you need to get a cross product of the array with itself, minus all the elements {x, y} | x == y. (i.e. an exhaustive list of unordered pairs of differing elements.) If you need ordered pairs of differing elements, then your code is best.

Sean U
  • 6,730
  • 1
  • 24
  • 43
0

As long as you say the above code is exactly what you want the following will be the implementation you want:

for(int i=0; i<pt.length; i++) {
  for(int j = i + 1; j<pt.length; j++) {
     /* do stuff */
  }
}

However, I have a god feeling you are actually interested in comparing the array values or am I wrong?

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Actually I'm running `pt[i].distance(pt[j]);` to set up a List of distances so I can find the shortest distance between a given list of points. – CaldwellYSR Mar 05 '12 at 17:06
  • You can do that faster using KD tree: http://en.wikipedia.org/wiki/K-d_tree. In case of 2D points it will reduce your algorithm from O(n^2) to O(n* sqrt(n)) – Boris Strandjev Mar 05 '12 at 17:08
0
for(int i=0; i<pt.length; i++) {
  for(int j=i+1; j<pt.length; j++) {
    if(pt[i] != pt[j]) { /* do stuff */ }
  }
}
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
0
for(int i=0; i<pt.length; i++) {
  for(int j=i; j--; ) {
     { /* do stuff */ }
  }
}

And, for completeness:

for(int i=pt.length; i--; ) {
  for(int j=i; j--; ) {
     { /* do stuff */ }
  }
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109