我目前正在尝试手动将 double 组按升序排序。我遇到的问题是输出仅在顶部列出了第一个最小值(这是正确的),但列出了给出的其余值 0.0。 (值范围从 -5 到 +20)。下面是我在排序方面的编码尝试。任何帮助将不胜感激。谢谢。
int index;
double temp;
for(index = 0; index < x.length; index++)
{
for(int j = 0; j < x.length - 1; j++)
{
if(x[j + 1] < x[j])
{
temp = x[j + 1];
x[j + 1] = x[j];
x[j] = temp;
}
}
}
最佳答案
这几乎就是你所得到的冒泡排序。试试这个:
public static void sort(int[] x) {
boolean sorted=true;
int temp;
while (sorted){
sorted = false;
for (int i=0; i < x.length-1; i++)
if (x[i] > x[i+1]) {
temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
sorted = true;
}
}
}
但是科林是对的。你最好使用 Arrays.sort。
关于java - 将 double 组按升序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660827/