sum = 0;
for (int i=1; i <= n; i++) {
for (int j=1; j <= n/i; j++)
sum = sum + j;
}
Note: Credit will not be given only for answers - show all your work: your reasoning, steps you took to get your answer etc.
SortablePointd[] pointsSortedByX = new SortablePointd [n];
// ... insert data ...
java.util.Arrays.sort (pointsSortedByX);
// Now pointsSortedByX is sorted by X.
What should SortablePointd look like? Something like this:
class SortablePointd implements Comparable {
// Constructor:
public SortablePointd (Pointd p, boolean sortByX)
{
}
public int compareTo (Object obj)
{
// ... Do the comparison based on whether it's by X or Y ...
}
// ...
}
Efficient sorting is an important part of the algorithm.
1. Sort points by X into array pointsSortedByX;
2. Sort points by Y into array pointsSortedByY;
3. dist = recursiveClosestPair (pointsSortedByX, 0, n-1, pointsSortedByY);
4. return dist;
// Handle bottom-out cases for recursion ... not shown ...
1. Identify median point in pointsSortedByX.
2. Move all points in pointsSortedByY whose X value is less or equal
than median's X into leftYList;
3. Move all points in pointsSortedByY whose X value is greater
than median's X into rightYList;
// Recurse:
4. leftDist = recursiveClosestPair (pointsSortedByX, left, median, leftYList);
5. rightDist = recursiveClosestPair (pointsSortedByX, median+1, right, rightYList);
6. minDist = min (leftDist, rightDist);
// Now worry about the band. These are the points
// whose x is within median-minDist to median+minDist
7. Go through pointsSortedByY and add to listOfPointsInBand;
8. if (no new points in band)
return minDist;
// Otherwise, need to check band points against each other.
9. For each band point, compute distance to next 7 points
and compare with minDist.
10. return minDist;
Submission: