@@ -179,25 +179,19 @@ private void computeNeighborhoods(float[][] centers, List<NeighborHood> neighbor
179179            }
180180        }
181181
182-         float [] scores  = new  float [clustersPerNeighborhood ];
183182        for  (int  i  = 0 ; i  < k ; i ++) {
184183            NeighborQueue  queue  = neighborQueues .get (i );
185-             int  neighborCount  = queue .size ();
186-             int [] neighbors  = new  int [neighborCount ];
187-             float  maxIntraDistance  = queue .consumeNodesWithWorstScore (neighbors , scores );
188-             // Sort neighbors by their score 
189-             for  (int  j  = 0 ; j  < neighborCount ; j ++) {
190-                 for  (int  l  = j  + 1 ; l  < neighborCount ; l ++) {
191-                     if  (scores [j ] > scores [l ]) {
192-                         // swap 
193-                         int  tmp  = neighbors [j ];
194-                         neighbors [j ] = neighbors [l ];
195-                         neighbors [l ] = tmp ;
196-                         float  tmpScore  = scores [j ];
197-                         scores [j ] = scores [l ];
198-                         scores [l ] = tmpScore ;
199-                     }
200-                 }
184+             if  (queue .size () == 0 ) {
185+                 // no neighbors, skip 
186+                 neighborhoods .set (i , NeighborHood .EMPTY );
187+                 continue ;
188+             }
189+             // consume the queue into the neighbors array and get the maximum intra-cluster distance 
190+             int [] neighbors  = new  int [queue .size ()];
191+             float  maxIntraDistance  = queue .topScore ();
192+             int  iter  = 0 ;
193+             while  (queue .size () > 0 ) {
194+                 neighbors [neighbors .length  - ++iter ] = queue .pop ();
201195            }
202196            NeighborHood  neighborHood  = new  NeighborHood (neighbors , maxIntraDistance );
203197            neighborhoods .set (i , neighborHood );
@@ -283,7 +277,9 @@ void cluster(FloatVectorValues vectors, KMeansIntermediate kMeansIntermediate) t
283277        cluster (vectors , kMeansIntermediate , false );
284278    }
285279
286-     record  NeighborHood (int [] neighbors , float  maxIntraDistance ) {}
280+     record  NeighborHood (int [] neighbors , float  maxIntraDistance ) {
281+         static  final  NeighborHood  EMPTY  = new  NeighborHood (new  int [0 ], Float .POSITIVE_INFINITY );
282+     }
287283
288284    /** 
289285     * cluster using a lloyd kmeans algorithm that also considers prior clustered neighborhoods when adjusting centroids 
0 commit comments