Skip to content

Commit eb3fc30

Browse files
committed
Remove unnecassary variable usage.
(cherry picked from commit ca51ac0)
1 parent 8885855 commit eb3fc30

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

divideconquer/ClosestPair.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public int xPartition(
9797
final Location[] a, final int first, final int last) {
9898

9999
Location pivot = a[last]; // pivot
100-
int pIndex = last;
101100
int i = first - 1;
102101
Location temp; // Temporarily store value for position transformation
103102
for (int j = first; j <= last - 1; j++) {
@@ -110,8 +109,8 @@ public int xPartition(
110109
}
111110
i++;
112111
temp = a[i]; // array[pivot] <-> array[i]
113-
a[i] = a[pIndex];
114-
a[pIndex] = temp;
112+
a[i] = a[last];
113+
a[last] = temp;
115114
return i; // pivot index
116115
}
117116

@@ -128,7 +127,6 @@ public int yPartition(
128127
final Location[] a, final int first, final int last) {
129128

130129
Location pivot = a[last]; // pivot
131-
int pIndex = last;
132130
int i = first - 1;
133131
Location temp; // Temporarily store value for position transformation
134132
for (int j = first; j <= last - 1; j++) {
@@ -141,8 +139,8 @@ public int yPartition(
141139
}
142140
i++;
143141
temp = a[i]; // array[pivot] <-> array[i]
144-
a[i] = a[pIndex];
145-
a[pIndex] = temp;
142+
a[i] = a[last];
143+
a[last] = temp;
146144
return i; // pivot index
147145
}
148146

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

195193
Location[] divideArray = new Location[indexNum];
196194
System.arraycopy(a, 0, divideArray, 0, indexNum); // Copy previous array
197-
int totalNum = indexNum; // number of coordinates in the divideArray
198195
int divideX = indexNum / 2; // Intermediate value for divide
199196
Location[] leftArray = new Location[divideX]; //divide - left array
200197
//divide-right array
201-
Location[] rightArray = new Location[totalNum - divideX];
198+
Location[] rightArray = new Location[indexNum - divideX];
202199
if (indexNum <= 3) { // If the number of coordinates is 3 or less
203200
return bruteForce(divideArray);
204201
}
205202
//divide-left array
206203
System.arraycopy(divideArray, 0, leftArray, 0, divideX);
207204
//divide-right array
208205
System.arraycopy(
209-
divideArray, divideX, rightArray, 0, totalNum - divideX);
206+
divideArray, divideX, rightArray, 0, indexNum - divideX);
210207

211208
double minLeftArea = 0; //Minimum length of left array
212209
double minRightArea = 0; //Minimum length of right array
213210
double minValue = 0; //Minimum lengt
214211

215212
minLeftArea = closestPair(leftArray, divideX); // recursive closestPair
216-
minRightArea = closestPair(rightArray, totalNum - divideX);
213+
minRightArea = closestPair(rightArray, indexNum - divideX);
217214
// window size (= minimum length)
218215
minValue = Math.min(minLeftArea, minRightArea);
219216

220217
// Create window. Set the size for creating a window
221218
// and creating a new array for the coordinates in the window
222-
for (int i = 0; i < totalNum; i++) {
219+
for (int i = 0; i < indexNum; i++) {
223220
double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x);
224221
if (xGap < minValue) {
225222
ClosestPair.setSecondCount(secondCount + 1); // size of the array
@@ -232,7 +229,7 @@ public double closestPair(final Location[] a, final int indexNum) {
232229
// new array for coordinates in window
233230
Location[] firstWindow = new Location[secondCount];
234231
int k = 0;
235-
for (int i = 0; i < totalNum; i++) {
232+
for (int i = 0; i < indexNum; i++) {
236233
double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x);
237234
if (xGap < minValue) { // if it's inside a window
238235
firstWindow[k] = divideArray[i]; // put in an array

0 commit comments

Comments
 (0)