[英]Why is this array not printing all it's values (Prints 0's)?
Basically the program generates positions of x,y,z coordinates, including a distance index. 基本上,程序会生成x,y,z坐标的位置,包括距离索引。 The program stores them in each specified index, and proceeds to sort them.
程序将它们存储在每个指定的索引中,并进行排序。 The resultant output is sorted but some columns print a bunch of zeroes (As if the array is never generated at that position).
结果输出已排序,但某些列显示一堆零(好像该位置从未生成过数组)。 Any help would be appreciated!
任何帮助,将不胜感激!
Example output : 示例输出:
(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)
Main program 主程序
//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;
}
}
You've mixed array initialization and sorting - that's the problem. 您混合了数组的初始化和排序-这就是问题所在。 On the first iteration, you fill the first element and then run sort, which moves this element to the end of the array, as it's greater than uninitialized zeros.
在第一个迭代中,您填充第一个元素,然后运行sort,将其移动到数组的末尾,因为它大于未初始化的零。 After sorting you print the first element which is now filled with zeros.
排序后,您将打印第一个元素,该元素现在填充了零。 This is still happening till the 6th iteration, when number of initialized elements becomes greater than number or uninitialized elements.
这种情况一直持续到第六次迭代,此时初始化元素的数量大于未初始化元素的数量。
You probably want to split the code in three parts: initialization, sorting, and printing. 您可能希望将代码分为三个部分:初始化,排序和打印。
You sort in the loop. 您在循环中排序。 This means the filled values are sorted to the end, and then later overwritten.
这意味着填充的值将被排序到末尾,然后被覆盖。
Fix: First do the i/j loop to fill, then sort once, then do a separate loop to print the data 修复:首先执行i / j循环以填充,然后排序一次,然后执行单独的循环以打印数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.