繁体   English   中英

如何对双精度数组进行冒泡排序?

[英]How do I bubble sort an array of doubles?

我是一个新手,我想使用冒泡排序算法对数组进行排序。 这就是我迄今为止所做的。

public class Storename {

public static void main(String[] args) {
    double[] revenues = {36372.92, 93784.52, 23466.24, 97744.98, 30243.70,   103362.26, 108232.71, 78513.01, 61711.97, 13268.60, 85281.88, 50308.06, 68102.39, 18335.74, 15146.26, 96230.22, 26291.95, 53778.41, 84727.77, 91674.64, 45650.94, 101584.65, 107373.77, 25650.34, 51512.09, 54565.04, 82806.54, 31565.73, 97256.94, 45216.76};

    bubbleSort(revenues);
}

private static void bubbleSort(double[] revenues) {
    int n = revenues.length;
    int temp = 0;

    for(int i=0; i < n; i++){
        for(int j=1; j < (n-i); j++){

            if(revenues[j-1] > revenues[j]){
                //swap the elements!
                temp = revenues[j-1];
                revenues[j-1] = revenues[j];
                revenues[j] = temp;
                System.out.print(revenues[i] + " ");
            }
        }
    }
}
}

你的 temp 应该是 double 而不是 int。
调用bubbleSort(revenues);后打印列表bubbleSort(revenues); 使用此代码:

for(double value:revenues)
        System.out.println(value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM