I've been writing a piece of code that will use a brute force approach to find 50 exponential equations that fit 5 points (x,y) that i've entered. The code is fine and works without a glitch.. The problem is when i try to make it keep a record of the 50 best equations. This sort routine is the one i've been using.. I've put it in its own test class just to make easier to follow. The current problem is it ends up displaying something completely unwarranted and the array ends up with only two different values.
public class sortTest{
public static void main(String [] args)
{
int [][] best = new int[50][3]; //50 best equations. the three placeholders are b,c,euclid in the equation y(t) = 10*b^(t*c). euclid is the euclidean distance between the equation and the points i've given it. The smaller it is, the better the equation.
int tempcomp [] = new int[3]; //to temporarily store new equations
boolean sorted;
for(int index = 0; index<best.length; index++){
best[index][0] = 100+index;
best[index][1] = 100+index;
best[index][2]= 100+index;
}
for(int c = 0; c<best.length; c++)
{
tempcomp[0] = 100-c;
tempcomp[1] = 100-c;
tempcomp[2]= 100-c;
if(tempcomp[2]<best[49][2])
best[49] = tempcomp;
sorted = false;
while (!sorted) {
sorted = true; // assume this is last pass over array
for (int index=0; index<best.length-1; index++) {
if (best[index][2] > best[index+1][2]) {
// exchange elements
tempcomp = best[index]; best[index] = best[index+1]; best[index+1] = tempcomp;
sorted = false; // after an exchange, must look again
}
}
}
}
}
}
Thanks for lookin!
Khodeir