diff --git a/divideconquer/ClosestPair.java b/divideconquer/ClosestPair.java index 67ebe89b..3d717417 100644 --- a/divideconquer/ClosestPair.java +++ b/divideconquer/ClosestPair.java @@ -97,7 +97,6 @@ public final class ClosestPair { final Location[] a, final int first, final int last) { Location pivot = a[last]; // pivot - int pIndex = last; int i = first - 1; Location temp; // Temporarily store value for position transformation for (int j = first; j <= last - 1; j++) { @@ -110,8 +109,8 @@ public final class ClosestPair { } i++; temp = a[i]; // array[pivot] <-> array[i] - a[i] = a[pIndex]; - a[pIndex] = temp; + a[i] = a[last]; + a[last] = temp; return i; // pivot index } @@ -128,7 +127,6 @@ public final class ClosestPair { final Location[] a, final int first, final int last) { Location pivot = a[last]; // pivot - int pIndex = last; int i = first - 1; Location temp; // Temporarily store value for position transformation for (int j = first; j <= last - 1; j++) { @@ -141,8 +139,8 @@ public final class ClosestPair { } i++; temp = a[i]; // array[pivot] <-> array[i] - a[i] = a[pIndex]; - a[pIndex] = temp; + a[i] = a[last]; + a[last] = temp; return i; // pivot index } @@ -194,11 +192,10 @@ public final class ClosestPair { Location[] divideArray = new Location[indexNum]; System.arraycopy(a, 0, divideArray, 0, indexNum); // Copy previous array - int totalNum = indexNum; // number of coordinates in the divideArray int divideX = indexNum / 2; // Intermediate value for divide Location[] leftArray = new Location[divideX]; //divide - left array //divide-right array - Location[] rightArray = new Location[totalNum - divideX]; + Location[] rightArray = new Location[indexNum - divideX]; if (indexNum <= 3) { // If the number of coordinates is 3 or less return bruteForce(divideArray); } @@ -206,20 +203,20 @@ public final class ClosestPair { System.arraycopy(divideArray, 0, leftArray, 0, divideX); //divide-right array System.arraycopy( - divideArray, divideX, rightArray, 0, totalNum - divideX); + divideArray, divideX, rightArray, 0, indexNum - divideX); double minLeftArea = 0; //Minimum length of left array double minRightArea = 0; //Minimum length of right array double minValue = 0; //Minimum lengt minLeftArea = closestPair(leftArray, divideX); // recursive closestPair - minRightArea = closestPair(rightArray, totalNum - divideX); + minRightArea = closestPair(rightArray, indexNum - divideX); // window size (= minimum length) minValue = Math.min(minLeftArea, minRightArea); // Create window. Set the size for creating a window // and creating a new array for the coordinates in the window - for (int i = 0; i < totalNum; i++) { + for (int i = 0; i < indexNum; i++) { double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); if (xGap < minValue) { ClosestPair.setSecondCount(secondCount + 1); // size of the array @@ -232,7 +229,7 @@ public final class ClosestPair { // new array for coordinates in window Location[] firstWindow = new Location[secondCount]; int k = 0; - for (int i = 0; i < totalNum; i++) { + for (int i = 0; i < indexNum; i++) { double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); if (xGap < minValue) { // if it's inside a window firstWindow[k] = divideArray[i]; // put in an array