在 K-Nearest 算法中获取最短的 'K' 距离 (Java)

Getting the shortest 'K' distances in K-Nearest algorithm (Java)

所以目前我有以下内容,它通过从具有计算距离的 "distance" 数组中获取最小距离值来找到我的 shortest/nearest 邻居。然后它进行另一次搜索以追踪它的索引,然后向我指示它属于哪个患者。

但是说我想找到 3 个最近的邻居,我该怎么做?我是否需要完全更改我的代码以适应这种情况?

非常感谢

    int min = 99;
    int d = 1;
    String diagnosis;
        //Finding smallest value from an array containing distance to new 'patient'
        for(d=1; d<= numberOFinstances; d++){
            if(distance[d] < min)
            min = distance[d];
        }

        for (int p = 1; p < numberOFinstances; p++) 
        {
         if (distance[p] == min){
            System.out.println("Nearest patient to new patient is Patient "+p+ " with a distance of: " + min);
            //Here I'm saying 6 because the diagnosis is in column 6 within the matrix
            diagnosis = data[p][6];
            System.out.println("The new patient's diagnosis is: " + diagnosis);
         }
        }

最好的方法是使用 Arrays.sort(int[])

Arrays.sort(distance);
int[] toReturn = new int[k];
for (int i = 0; i < k; i++) {
  toReturn[i] = distance[i];
}