基本上,程序向用户询问(X,Y,Z)坐标的范围,并询问他希望生成的点数。这些值将传递给对象类,在该对象类中进行快速排序算法。一旦将值传递给writeData类,它们就会继续在文件中打印一个示例输出:其中点数是2(2组xyz坐标) 范围是1(从正无穷大到负无穷大)
(0.01916820621893911),(0.7031915303569696),(0.8313160912583086)
(-0.9343528090486088),(0.015998642441189093),(0.49980249751031924)
正如您可以通过输出判断的那样,它根本没有排序,如果我的算法似乎有效,为什么会出现此问题。我在过去一小时左右尝试过调试,但无济于事。此外,由于快速排序mufti维数组并不经常使用,这是我可以尝试使用的唯一算法。无论如何,一些或任何输入将帮助我解决这个难题!
主要节目(短而甜):
import java.io.*;
public class test {
public static void main (String [] args) throws IOException {
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
testplus1 c = new testplus1();
double dataPoints [][] = new double [3][10000];
int numPoints;
int range;
String input;
System.out.println("Please enter a range");
input = myInput.readLine();
range = Integer.parseInt(input);
System.out.println("Please enter the number of points");
input = myInput.readLine();
numPoints = Integer.parseInt(input);
c.setNumPoints(numPoints);
c.setArray(dataPoints);
c.setRange(range);
c.fillArray(dataPoints,range,numPoints);
}
}
对象类
import java.io.*;
public class testplus1 {
private static double [][] myDataPoints;
private static double [][] sd;
private static int myRange;
private static int myNumPoints;
public testplus1() throws IOException{
myNumPoints = getNumPoints();
myRange = getRange();
myDataPoints = getArray();
fillArray(myDataPoints,myRange,myNumPoints);
}
public void fillArray(double [][] myDataPoints,int myRange, int myNumPoints) throws IOException{
for(int i = 0; i < myNumPoints; i++) {
myDataPoints[0][i] = 2* (double)(Math.random () * myRange) - myRange;
myDataPoints[1][i] = 2* (double)(Math.random () * myRange) - myRange;
myDataPoints[2][i] = 2* (double)(Math.random () * myRange) - myRange;
sort(myDataPoints,0,0,myDataPoints.length-1);
}
}
public double[][] sort(double[][] array, int key, int down, int top) throws IOException{
double[][] a = new double[array.length][3];
System.arraycopy(array,0,a,0, a.length);
int i = down;
int j = top;
double x = a[(down + top) / 2][key];
do {
while (a[i][key] < x) {
i++;
}
while (a[j][key] > x) {
j--;
}
if (i <= j) {
double[] temp = new double[a[i].length];
for (int y = 0; y < a[i].length; y++) {
temp[y] = a[i][y];
a[i][y] = a[j][y];
a[j][y] = temp[y];
}
i++;
j--;
}
} while (i <= j);
if (down < j) {
a = sort(a, key, down, j);
}
if (i < top) {
a = sort(a, key, i, top);
}
writeData(myDataPoints);
return a;
}
public static void writeData(double [][] myDataPoints) throws IOException {
PrintWriter printWriter = (new PrintWriter ("test123"));
for(int i = 0; i < myNumPoints; i++) {
printWriter.println("(" + myDataPoints[0][i] + "),(" + myDataPoints[1][i] + "),(" + myDataPoints[2][i] + ")");
}
printWriter.close();
}
public void setArray(double [][] dataPoints) {
myDataPoints = dataPoints;
}
public double [][] getArray() {
return myDataPoints;
}
public void setNumPoints(int numPoints) {
myNumPoints = numPoints;
}
public int getNumPoints() {
return myNumPoints;
}
public void setRange(int range) {
myRange = range;
}
public int getRange() {
return myRange;
}
}
答案 0 :(得分:0)
查看您的sort
方法。
public double[][] sort(double[][] array, int key, int down, int top) throws IOException{
double[][] a = new double[array.length][3];
System.arraycopy(array,0,a,0, a.length);
您创建一个新数组a
并对其执行所有操作而不是原始输入数组(对原始数组的更改将在方法外自动显示)。最后,您返回a
,这将有效,但在fillArray
...
sort(myDataPoints,0,0,myDataPoints.length-1);
您不会将其分配给任何变量。唯一可以改变的是myDataPoints
的副本,而不是mydataPoints
本身。
答案 1 :(得分:0)
您应该使用Arrays.sort
。使用myDataPoints
作为第一个参数,使用自定义比较器作为第二个参数。有关详细信息,请查看http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[], java.util.Comparator)处的文档。
如果您向我提供有关如何对值进行排序的更多信息,我可以举例说明比较器。