基本上,该程序会生成 x、y、z 坐标的位置,包括距离索引。程序将它们存储在每个指定的索引中,并继续对它们进行排序。结果输出已排序,但有些列打印一堆零(好像从未在该位置生成数组)。任何帮助,将不胜感激!
示例输出:
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(2.1933026),(3.873999),(2.6528487)),(5.1822824)
(2.1933026),(3.873999),(4.9137025)),(6.6304536)
(3.4213266),(5.4315333),(5.976922)),(8.771011)
(4.084449),(5.4315333),(5.976922)),(9.050297)
(6.4669027),(5.4315333),(5.976922)),(10.346303)
主程序
//import java.util.ArrayList;
import java.util.*;
public class MyQuickSort {
private static float a[][] = new float [10][4];
public static void main(String[] args) {
System.out.println("(" + "X Length" + "),(" + "Y Length" + "),(" + "Z Length" + "),(" + "Distance Length" + ")");
for(int i = 0; i < a.length ; i++) {
for(int j = 0; j < 3; j++) {
a[i][j] = (float)(Math.random () * 6) +1 ;
//a[i][3] = (float)Math.sqrt((a[i][0] * a[i][0]) + (a[i][1] * a[i][1]) + (a[i][2] * a[i][2]));
for (int k = 0; k < 4; k++ ) {
quickSort(a, 0, a.length - 1,k);
}
}
a[i][3] = (float)Math.sqrt((a[i][0] * a[i][0]) + (a[i][1] * a[i][1]) + (a[i][2] * a[i][2]));
System.out.println("(" + a[i][0] + "),(" + a[i][1] + "),(" + a[i][2] + ")" + "),(" + a[i][3] + ")");
}
}
public static void quickSort(float[][] a, int p, int r,int k)
{
if(p<r)
{
int q=partition(a,p,r,k);
quickSort(a,p,q,k);
quickSort(a,q+1,r,k);
}
}
private static int partition(float[][] a, int p, int r,int k) {
float d = a[p][k];
int i = p-1 ;
int j = r+1 ;
while (true) {
i++;
while ( i< r && a[i][k] < d)
i++;
j--;
while (j>p && a[j][k] > d)
j--;
if (i < j)
swap(a, i, j,k);
else
return j;
}
}
private static void swap(float[][] a, int i, int j, int k) {
float temp = a[i][k];
a[i][k] = a[j][k];
a[j][k] = temp;
}
}