基本上,程序要求用户提供 (X,Y,Z) 坐标的范围,并要求用户提供他希望生成的点数。这些值被传递到对象类,在该类中进行快速排序算法。将值传递给 writeData 类后,它们会继续在文件中打印示例输出:其中点数为 2(2 组 xyz 坐标),范围为 1(从正无穷大到负无穷大)
(0.01916820621893911),(0.7031915303569696),(0.8313160912583086)
(-0.9343528090486088),(0.015998642441189093),(0.49980249751031924)
从输出可以看出,它根本没有排序,如果我的算法似乎有效,为什么会出现这个问题。我已经尝试调试过去一个小时左右,但无济于事。此外,由于快速排序多维数组并没有真正经常使用,这是我可以尝试使用的唯一算法。无论如何,一些或任何输入都会帮助我解决这个难题!
主程序(短而甜):
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;
}
}