I am a newbie and I want to sort an array with the use of bubblesort algorithm. This is what I've done till now.
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] + " ");
}
}
}
}
}
Your temp should be double instead of int.
print the list after calling bubbleSort(revenues);
use this code:
for(double value:revenues)
System.out.println(value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.