2016-09-30 77 views
-1

我是新手,我想使用bubblesort算法對數組進行排序。這是我迄今爲止所做的。如何對一個雙精度數組進行排序?

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] + " "); 
      } 
     } 
    } 
} 
} 
+0

完全相同的方式,你冒泡排序別的:調用bubbleSort(revenues);使用這個代碼後
打印清單。只有數據類型改變。 – EJP

回答

0

你的temp應該是double而不是int。

for(double value:revenues) 
     System.out.println(value); 
相關問題