Bubble Sort in Java



public class BubbleSort {

 public static void main(String[] args) {
  int[] inputNumbers = { 15, 80, 76, 111, 99, 0, 40, 12, 10, 01 };

  int len = inputNumbers.length;
  int n;
  for (int index = len; index >= 0; index--) {
   for (int i = 0; i < len - 1; i++) {
    n = i + 1;
    if (inputNumbers[i] > inputNumbers[n]) {
     // swapping
     int _temp;
     _temp = inputNumbers[i];
     inputNumbers[i] = inputNumbers[n];
     inputNumbers[n] = _temp;
    }
   }

   for (int i = 0; i < inputNumbers.length; i++) {
    System.out.print(inputNumbers[i] + "  ");
   }
   System.out.println("\n");
  }

 }

}

Output:

15  76  80  99  0  40  12  10  1  111  

15  76  80  0  40  12  10  1  99  111  

15  76  0  40  12  10  1  80  99  111  

15  0  40  12  10  1  76  80  99  111  

0  15  12  10  1  40  76  80  99  111  

0  12  10  1  15  40  76  80  99  111  

0  10  1  12  15  40  76  80  99  111  

0  1  10  12  15  40  76  80  99  111  

0  1  10  12  15  40  76  80  99  111  

0  1  10  12  15  40  76  80  99  111 








 
SHARE

About Unknown

    Blogger Comment
    Facebook Comment

1 comments :