我是新手,我想使用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 :(得分:0)
你的温度应该是双倍而不是int
调用bubbleSort(revenues);
后打印列表使用此代码:
for(double value:revenues)
System.out.println(value);