Skip to content

Hacktoberfest: Remove unnecassary variable usage. #1596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions divideconquer/ClosestPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public int xPartition(
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++) {
Expand All @@ -110,8 +109,8 @@ public int xPartition(
}
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
}

Expand All @@ -128,7 +127,6 @@ public int yPartition(
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++) {
Expand All @@ -141,8 +139,8 @@ public int yPartition(
}
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
}

Expand Down Expand Up @@ -194,32 +192,31 @@ public double closestPair(final Location[] a, final int indexNum) {

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);
}
//divide-left array
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
Expand All @@ -232,7 +229,7 @@ public double closestPair(final Location[] a, final int indexNum) {
// 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
Expand Down