1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
/* Java Bubble Sort Descending Order Example This Java bubble sort example shows how to sort an array of int in descending order using bubble sort algorithm. */ public class BubbleSortDescendingOrder { public static void main(String[] args) { //create an int array we want to sort using bubble sort algorithm int intArray[] = new int[]{5,90,35,45,150,3}; //print array before sorting using bubble sort algorithm System.out.println("Array Before Bubble Sort"); for(int i=0; i < intArray.length; i++){ System.out.print(intArray[i] + " "); } //sort an array in descending order using bubble sort algorithm bubbleSort(intArray); System.out.println(""); //print array after sorting using bubble sort algorithm System.out.println("Array After Bubble Sort"); for(int i=0; i < intArray.length; i++){ System.out.print(intArray[i] + " "); } } private static void bubbleSort(int[] intArray) { /* * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the next one. * Element is swapped with the next element if the next element is smaller. * * Bubble sort steps are as follows. * * 1. Compare array[0] & array[1] * 2. If array[0] < array [1] swap it. * 3. Compare array[1] & array[2] * 4. If array[1] < array[2] swap it. * ... * 5. Compare array[n-1] & array[n] * 6. if [n-1] < array[n] then swap it. * * After this step we will have smallest element at the last index. * * Repeat the same steps for array[1] to array[n-1] * */ int n = intArray.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(intArray[j-1] < intArray[j]){ //swap the elements! temp = intArray[j-1]; intArray[j-1] = intArray[j]; intArray[j] = temp; } } } } } /* Output of the Bubble Sort Descending Order Example would be Array Before Bubble Sort 5 90 35 45 150 3 Array After Bubble Sort 150 90 45 35 5 3 */ |
Thanks for taking the time to post this example. I have used it but I find that the last element is not getting sorted:
int[] sortArray = new int[]{3, 5, 1, 6, 8, 2, 7, 12, 10, 9};
for(int i=0; i<sortarray.length; i++){=”” for(int=”” j=”1;” j<(sortarray.length-1);=”” j++){=”” if(sortarray[j-1]=”” <=”” sortarray[j]){=”” int=”” temp=”sortArray[j-1];” sortarray[j-1]=”sortArray[j];” sortarray[j]=”temp;” }=”” }=”” }=”” this=”” produces=”” the=”” following:=”” [12,=”” 10,=”” 8,=”” 7,=”” 6,=”” 5,=”” 3,=”” 2,=”” 1,=”” 9]=””>
I had been trying to reverse my array to descending order for literally 20 hours until I came across your example!! very useful…Thanx again
You can also make it go backwards. However you need to place a limit for that as well…
intArray.length………………….what is the purpose of this
NNNNNNNNNNNOOOOOOOOOOOOOOIIIIIIIIIIIIIIIIIIIIIIIIIIIICCCCCCCCCCCCCEEEEEEEEEEEEEEEEE
To check the last element or complete loop, I think The J loop must have (j <n) rather=”” then=”” (j=”” <=”” n-1).=”” enjoy=”” coding=”” br=”” mohammed=”” nadeem=”” bari=”” write2nadeem(at)live.com=””>
Thanks for taking the time to post this example 🙂
I have done (for fun, i like programming) the same example here :
public class triTab {
public static void trier(int intArray[]){
for(int z = 0; z<intarray.length;z++){ for(int=”” i=”0;(intArray.length-1)”>i;i++){
if(intArray[i]>intArray[i+1]){
int tempon= intArray[i+1];
intArray[i+1]=intArray[i];
intArray[i]=tempon;
}
}
}
}
public static void afficher(int tableauTrier [] ){
int i=0;
while(i<(tableauTrier.length)){
System.out.println(tableauTrier[i]);
i++;
}
}
/**
* @author Florian Brunet
* @country France
* @param args
*/
public static void main(String[] args) {
int tableauATrier [] ;//= new int[2];
tableauATrier=new int[]{6,2,4,1,5,888,10,3};
System.out.println(“tableauATrier”);
afficher(tableauATrier);
System.out.println(“fin”);
trier(tableauATrier);
System.out.println(“tableauTrier”);
afficher(tableauATrier);
}
}
this sorts in ascending not descending