@@ -97,7 +97,6 @@ public int xPartition(
97
97
final Location [] a , final int first , final int last ) {
98
98
99
99
Location pivot = a [last ]; // pivot
100
- int pIndex = last ;
101
100
int i = first - 1 ;
102
101
Location temp ; // Temporarily store value for position transformation
103
102
for (int j = first ; j <= last - 1 ; j ++) {
@@ -110,8 +109,8 @@ public int xPartition(
110
109
}
111
110
i ++;
112
111
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 ;
115
114
return i ; // pivot index
116
115
}
117
116
@@ -128,7 +127,6 @@ public int yPartition(
128
127
final Location [] a , final int first , final int last ) {
129
128
130
129
Location pivot = a [last ]; // pivot
131
- int pIndex = last ;
132
130
int i = first - 1 ;
133
131
Location temp ; // Temporarily store value for position transformation
134
132
for (int j = first ; j <= last - 1 ; j ++) {
@@ -141,8 +139,8 @@ public int yPartition(
141
139
}
142
140
i ++;
143
141
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 ;
146
144
return i ; // pivot index
147
145
}
148
146
@@ -194,32 +192,31 @@ public double closestPair(final Location[] a, final int indexNum) {
194
192
195
193
Location [] divideArray = new Location [indexNum ];
196
194
System .arraycopy (a , 0 , divideArray , 0 , indexNum ); // Copy previous array
197
- int totalNum = indexNum ; // number of coordinates in the divideArray
198
195
int divideX = indexNum / 2 ; // Intermediate value for divide
199
196
Location [] leftArray = new Location [divideX ]; //divide - left array
200
197
//divide-right array
201
- Location [] rightArray = new Location [totalNum - divideX ];
198
+ Location [] rightArray = new Location [indexNum - divideX ];
202
199
if (indexNum <= 3 ) { // If the number of coordinates is 3 or less
203
200
return bruteForce (divideArray );
204
201
}
205
202
//divide-left array
206
203
System .arraycopy (divideArray , 0 , leftArray , 0 , divideX );
207
204
//divide-right array
208
205
System .arraycopy (
209
- divideArray , divideX , rightArray , 0 , totalNum - divideX );
206
+ divideArray , divideX , rightArray , 0 , indexNum - divideX );
210
207
211
208
double minLeftArea = 0 ; //Minimum length of left array
212
209
double minRightArea = 0 ; //Minimum length of right array
213
210
double minValue = 0 ; //Minimum lengt
214
211
215
212
minLeftArea = closestPair (leftArray , divideX ); // recursive closestPair
216
- minRightArea = closestPair (rightArray , totalNum - divideX );
213
+ minRightArea = closestPair (rightArray , indexNum - divideX );
217
214
// window size (= minimum length)
218
215
minValue = Math .min (minLeftArea , minRightArea );
219
216
220
217
// Create window. Set the size for creating a window
221
218
// 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 ++) {
223
220
double xGap = Math .abs (divideArray [divideX ].x - divideArray [i ].x );
224
221
if (xGap < minValue ) {
225
222
ClosestPair .setSecondCount (secondCount + 1 ); // size of the array
@@ -232,7 +229,7 @@ public double closestPair(final Location[] a, final int indexNum) {
232
229
// new array for coordinates in window
233
230
Location [] firstWindow = new Location [secondCount ];
234
231
int k = 0 ;
235
- for (int i = 0 ; i < totalNum ; i ++) {
232
+ for (int i = 0 ; i < indexNum ; i ++) {
236
233
double xGap = Math .abs (divideArray [divideX ].x - divideArray [i ].x );
237
234
if (xGap < minValue ) { // if it's inside a window
238
235
firstWindow [k ] = divideArray [i ]; // put in an array
0 commit comments