code: private void findNeighbor(int p) {
          // if no neighbors available, set flag for UpdatePoint to find
          if (npoints == 1) {
              neighbor[p] = p;
              distance[p] = Float.MAX_VALUE;
              return;
          }
  
          // find first point unequal to p itself
          int first = 0;
          if (p == points[first]) {
              first = 1;
          }
  
          neighbor[p] = points[first];
          distance[p] = linkage.d(p, neighbor[p]);
  
          // now test whether each other point is closer
          for (int i = first + 1; i < npoints; i++) {
              int q = points[i];
              if (q != p) {
                  float d = linkage.d(p, q);
                  if (d < distance[p]) {
                      distance[p] = d;
                      neighbor[p] = q;
                  }
              }
          }
      }
docs: Find nearest neighbor of a given point.