按升序对双精度数组排序 [英] Sorting double arrays into ascending order

查看:89
本文介绍了按升序对双精度数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试手动将Double Array排序为升序。我遇到的问题是,输出仅在顶部列出第一个最小值(这是正确的),但列出了其余的0.0值。 (值的范围是-5到+20)。以下是我对排序的编码尝试。任何帮助将不胜感激。谢谢。

I'm currently trying to sort a Double Array into ascending order manually. The problem I'm having is that the output only lists the 1st smallest value at the top (which is correct), but lists the rest of the values given as 0.0. (The values range from -5 to +20). Below is my coding attempt at sorting. Any help will be greatly appreciated. Thank you.

             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;  
                           }
                      }
                }


推荐答案

那差不多是您到达那里的泡泡。试试这个:

That´s almost bubblesort you got there. try this :

 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。

But Collin is right. You are better off with Arrays.sort.

这篇关于按升序对双精度数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆