Bubble Sort Example

             Bubble Sort Example /*         Java Bubble Sort Example         This Java bubble sort example shows how to...


            Bubble Sort Example

  1. /*
  2.         Java Bubble Sort Example
  3.         This Java bubble sort example shows how to sort an array of int using bubble
  4.         sort algorithm. Bubble sort is the simplest sorting algorithm.
  5. */
  6. public class BubbleSort {
  7.         public static void main(String[] args) {
  8.                
  9.                 //create an int array we want to sort using bubble sort algorithm
  10.                 int intArray[] = new int[]{5,90,35,45,150,3};
  11.                
  12.                 //print array before sorting using bubble sort algorithm
  13.                 System.out.println("Array Before Bubble Sort");
  14.                 for(int i=0; i < intArray.length; i++){
  15.                         System.out.print(intArray[i] + " ");
  16.                 }
  17.                
  18.                 //sort an array using bubble sort algorithm
  19.                 bubbleSort(intArray);
  20.                
  21.                 System.out.println("");
  22.                
  23.                 //print array after sorting using bubble sort algorithm
  24.                 System.out.println("Array After Bubble Sort");
  25.                 for(int i=0; i < intArray.length; i++){
  26.                         System.out.print(intArray[i] + " ");
  27.                 }
  28.         }
  29.         private static void bubbleSort(int[] intArray) {
  30.                
  31.                 /*
  32.                  * In bubble sort, we basically traverse the array from first
  33.                  * to array_length - 1 position and compare the element with the next one.
  34.                  * Element is swapped with the next element if the next element is greater.
  35.                  *
  36.                  * Bubble sort steps are as follows.
  37.                  *
  38.                  * 1. Compare array[0] & array[1]
  39.                  * 2. If array[0] > array [1] swap it.
  40.                  * 3. Compare array[1] & array[2]
  41.                  * 4. If array[1] > array[2] swap it.
  42.                  * ...
  43.                  * 5. Compare array[n-1] & array[n]
  44.                  * 6. if [n-1] > array[n] then swap it.
  45.                  *
  46.                  * After this step we will have largest element at the last index.
  47.                  *
  48.                  * Repeat the same steps for array[1] to array[n-1]
  49.                  *  
  50.                  */
  51.                
  52.                 int n = intArray.length;
  53.                 int temp = 0;
  54.                
  55.                 for(int i=0; i < n; i++){
  56.                         for(int j=1; j < (n-i); j++){
  57.                                
  58.                                 if(intArray[j-1] > intArray[j]){
  59.                                         //swap the elements!
  60.                                         temp = intArray[j-1];
  61.                                         intArray[j-1] = intArray[j];
  62.                                         intArray[j] = temp;
  63.                                 }
  64.                                
  65.                         }
  66.                 }
  67.        
  68.         }
  69. }
  70. /*
  71. Output of the Bubble Sort Example would be
  72. Array Before Bubble Sort
  73. 5 90 35 45 150 3
  74. Array After Bubble Sort
  75. 3 5 35 45 90 150
  76. */

Bubble Sort Example

                Bubble Sort Example /*         Java Bubble Sort Example         This Java bubble sort example shows how...


                Bubble Sort Example

  1. /*
  2.         Java Bubble Sort Example
  3.         This Java bubble sort example shows how to sort an array of int using bubble
  4.         sort algorithm. Bubble sort is the simplest sorting algorithm.
  5. */
  6. public class BubbleSort {
  7.         public static void main(String[] args) {
  8.                
  9.                 //create an int array we want to sort using bubble sort algorithm
  10.                 int intArray[] = new int[]{5,90,35,45,150,3};
  11.                
  12.                 //print array before sorting using bubble sort algorithm
  13.                 System.out.println("Array Before Bubble Sort");
  14.                 for(int i=0; i < intArray.length; i++){
  15.                         System.out.print(intArray[i] + " ");
  16.                 }
  17.                
  18.                 //sort an array using bubble sort algorithm
  19.                 bubbleSort(intArray);
  20.                
  21.                 System.out.println("");
  22.                
  23.                 //print array after sorting using bubble sort algorithm
  24.                 System.out.println("Array After Bubble Sort");
  25.                 for(int i=0; i < intArray.length; i++){
  26.                         System.out.print(intArray[i] + " ");
  27.                 }
  28.         }
  29.         private static void bubbleSort(int[] intArray) {
  30.                
  31.                 /*
  32.                  * In bubble sort, we basically traverse the array from first
  33.                  * to array_length - 1 position and compare the element with the next one.
  34.                  * Element is swapped with the next element if the next element is greater.
  35.                  *
  36.                  * Bubble sort steps are as follows.
  37.                  *
  38.                  * 1. Compare array[0] & array[1]
  39.                  * 2. If array[0] > array [1] swap it.
  40.                  * 3. Compare array[1] & array[2]
  41.                  * 4. If array[1] > array[2] swap it.
  42.                  * ...
  43.                  * 5. Compare array[n-1] & array[n]
  44.                  * 6. if [n-1] > array[n] then swap it.
  45.                  *
  46.                  * After this step we will have largest element at the last index.
  47.                  *
  48.                  * Repeat the same steps for array[1] to array[n-1]
  49.                  *  
  50.                  */
  51.                
  52.                 int n = intArray.length;
  53.                 int temp = 0;
  54.                
  55.                 for(int i=0; i < n; i++){
  56.                         for(int j=1; j < (n-i); j++){
  57.                                
  58.                                 if(intArray[j-1] > intArray[j]){
  59.                                         //swap the elements!
  60.                                         temp = intArray[j-1];
  61.                                         intArray[j-1] = intArray[j];
  62.                                         intArray[j] = temp;
  63.                                 }
  64.                                
  65.                         }
  66.                 }
  67.        
  68.         }
  69. }
  70. /*
  71. Output of the Bubble Sort Example would be
  72. Array Before Bubble Sort
  73. 5 90 35 45 150 3
  74. Array After Bubble Sort
  75. 3 5 35 45 90 150
  76. */

Bubble Sort Descending Order Example

          Bubble Sort Descending Order Example /*         Java Bubble Sort Descending Order Example         This Java b...


          Bubble Sort Descending Order Example

  1. /*
  2.         Java Bubble Sort Descending Order Example
  3.         This Java bubble sort example shows how to sort an array of int in descending
  4.         order using bubble sort algorithm.
  5. */
  6. public class BubbleSortDescendingOrder {
  7.         public static void main(String[] args) {
  8.                
  9.                 //create an int array we want to sort using bubble sort algorithm
  10.                 int intArray[] = new int[]{5,90,35,45,150,3};
  11.                
  12.                 //print array before sorting using bubble sort algorithm
  13.                 System.out.println("Array Before Bubble Sort");
  14.                 for(int i=0; i < intArray.length; i++){
  15.                         System.out.print(intArray[i] + " ");
  16.                 }
  17.                
  18.                 //sort an array in descending order using bubble sort algorithm
  19.                 bubbleSort(intArray);
  20.                
  21.                 System.out.println("");
  22.                
  23.                 //print array after sorting using bubble sort algorithm
  24.                 System.out.println("Array After Bubble Sort");
  25.                 for(int i=0; i < intArray.length; i++){
  26.                         System.out.print(intArray[i] + " ");
  27.                 }
  28.         }
  29.         private static void bubbleSort(int[] intArray) {
  30.                
  31.                 /*
  32.                  * In bubble sort, we basically traverse the array from first
  33.                  * to array_length - 1 position and compare the element with the next one.
  34.                  * Element is swapped with the next element if the next element is smaller.
  35.                  *
  36.                  * Bubble sort steps are as follows.
  37.                  *
  38.                  * 1. Compare array[0] & array[1]
  39.                  * 2. If array[0] < array [1] swap it.
  40.                  * 3. Compare array[1] & array[2]
  41.                  * 4. If array[1] < array[2] swap it.
  42.                  * ...
  43.                  * 5. Compare array[n-1] & array[n]
  44.                  * 6. if [n-1] < array[n] then swap it.
  45.                  *
  46.                  * After this step we will have smallest element at the last index.
  47.                  *
  48.                  * Repeat the same steps for array[1] to array[n-1]
  49.                  *  
  50.                  */
  51.                
  52.                 int n = intArray.length;
  53.                 int temp = 0;
  54.                
  55.                 for(int i=0; i < n; i++){
  56.                         for(int j=1; j < (n-i); j++){
  57.                                
  58.                                 if(intArray[j-1] < intArray[j]){
  59.                                         //swap the elements!
  60.                                         temp = intArray[j-1];
  61.                                         intArray[j-1] = intArray[j];
  62.                                         intArray[j] = temp;
  63.                                 }
  64.                                
  65.                         }
  66.                 }
  67.        
  68.         }
  69. }
  70. /*
  71. Output of the Bubble Sort Descending Order Example would be
  72. Array Before Bubble Sort
  73. 5 90 35 45 150 3
  74. Array After Bubble Sort
  75. 150 90 45 35 5 3
  76. */