Bubble Sort Using JAVA

  1. Home
  2. Blog
  3. Bubble Sort Using JAVA

Bubble Sort Using JAVA

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through list to be sorted, comparing each pair of adjacent items and swapping them if they are out of order. The pass through list is repeated until no swaps are needed, which indicates that the list is now sorted. Although algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

BubbleSort

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Therefore, bubble sort is not a practical sorting algorithm when n is large.Performance of bubble sort over an already-sorted list (best-case) is O(n).

package com.ms;

import java.util.Arrays;

public class BubbleSort {

  public static void main(String[] args) {
    int num[] = {87,33,5,69,92,9,66,8,49,23};
    System.out.println("Before Sort:: " + Arrays.toString(num));
    applyBubbleSort(num);
    System.out.println("After Sort:: " + Arrays.toString(num));
  }

  public static void applyBubbleSort( int [ ] num){
    System.out.println("Total Elements:: " + num.length);
    for (int i = num.length - 1; i > 0; i--) {
      System.out.println("Value of i :: " + i);
           for (int j = 0; j < i; j++) {
          	 System.out.println("Value of j --> " + j);
          	 System.out.println("Is " + num[ j ] + " > " + num[ j + 1]);
          	 
               if (num[ j ] > num[ j + 1] ) {
              	 System.out.println("Swap values");
                   swapValues( num , j , j + 1 );
                   System.out.println("Current Sort Order:: " + Arrays.toString(num));
               }else{
              	 System.out.println("No Swap");
               }
           }
       }
  }
  
  public static void swapValues(int[] array, int i, int j) {
       int temp;
       temp = array[ i ];
       array[ i ] = array[ j ];
       array[ j ] = temp;
  } 
}

 

Before Sort:: [87, 33, 5, 69, 92, 9, 66, 8, 49, 23]
Total Elements:: 10
Value of i :: 9
Value of j --> 0
Is 87 > 33
Swap values
Current Sort Order:: [33, 87, 5, 69, 92, 9, 66, 8, 49, 23]
Value of j --> 1
Is 87 > 5
Swap values
Current Sort Order:: [33, 5, 87, 69, 92, 9, 66, 8, 49, 23]
Value of j --> 2
Is 87 > 69
Swap values
Current Sort Order:: [33, 5, 69, 87, 92, 9, 66, 8, 49, 23]
Value of j --> 3
Is 87 > 92
No Swap
Value of j --> 4
Is 92 > 9
Swap values
Current Sort Order:: [33, 5, 69, 87, 9, 92, 66, 8, 49, 23]
Value of j --> 5
Is 92 > 66
Swap values
Current Sort Order:: [33, 5, 69, 87, 9, 66, 92, 8, 49, 23]
Value of j --> 6
Is 92 > 8
Swap values
Current Sort Order:: [33, 5, 69, 87, 9, 66, 8, 92, 49, 23]
Value of j --> 7
Is 92 > 49
Swap values
Current Sort Order:: [33, 5, 69, 87, 9, 66, 8, 49, 92, 23]
Value of j --> 8
Is 92 > 23
Swap values
Current Sort Order:: [33, 5, 69, 87, 9, 66, 8, 49, 23, 92]
Value of i :: 8
Value of j --> 0
Is 33 > 5
Swap values
Current Sort Order:: [5, 33, 69, 87, 9, 66, 8, 49, 23, 92]
Value of j --> 1
Is 33 > 69
No Swap
Value of j --> 2
Is 69 > 87
No Swap
Value of j --> 3
Is 87 > 9
Swap values
Current Sort Order:: [5, 33, 69, 9, 87, 66, 8, 49, 23, 92]
Value of j --> 4
Is 87 > 66
Swap values
Current Sort Order:: [5, 33, 69, 9, 66, 87, 8, 49, 23, 92]
Value of j --> 5
Is 87 > 8
Swap values
Current Sort Order:: [5, 33, 69, 9, 66, 8, 87, 49, 23, 92]
Value of j --> 6
Is 87 > 49
Swap values
Current Sort Order:: [5, 33, 69, 9, 66, 8, 49, 87, 23, 92]
Value of j --> 7
Is 87 > 23
Swap values
Current Sort Order:: [5, 33, 69, 9, 66, 8, 49, 23, 87, 92]
Value of i :: 7
Value of j --> 0
Is 5 > 33
No Swap
Value of j --> 1
Is 33 > 69
No Swap
Value of j --> 2
Is 69 > 9
Swap values
Current Sort Order:: [5, 33, 9, 69, 66, 8, 49, 23, 87, 92]
Value of j --> 3
Is 69 > 66
Swap values
Current Sort Order:: [5, 33, 9, 66, 69, 8, 49, 23, 87, 92]
Value of j --> 4
Is 69 > 8
Swap values
Current Sort Order:: [5, 33, 9, 66, 8, 69, 49, 23, 87, 92]
Value of j --> 5
Is 69 > 49
Swap values
Current Sort Order:: [5, 33, 9, 66, 8, 49, 69, 23, 87, 92]
Value of j --> 6
Is 69 > 23
Swap values
Current Sort Order:: [5, 33, 9, 66, 8, 49, 23, 69, 87, 92]
Value of i :: 6
Value of j --> 0
Is 5 > 33
No Swap
Value of j --> 1
Is 33 > 9
Swap values
Current Sort Order:: [5, 9, 33, 66, 8, 49, 23, 69, 87, 92]
Value of j --> 2
Is 33 > 66
No Swap
Value of j --> 3
Is 66 > 8
Swap values
Current Sort Order:: [5, 9, 33, 8, 66, 49, 23, 69, 87, 92]
Value of j --> 4
Is 66 > 49
Swap values
Current Sort Order:: [5, 9, 33, 8, 49, 66, 23, 69, 87, 92]
Value of j --> 5
Is 66 > 23
Swap values
Current Sort Order:: [5, 9, 33, 8, 49, 23, 66, 69, 87, 92]
Value of i :: 5
Value of j --> 0
Is 5 > 9
No Swap
Value of j --> 1
Is 9 > 33
No Swap
Value of j --> 2
Is 33 > 8
Swap values
Current Sort Order:: [5, 9, 8, 33, 49, 23, 66, 69, 87, 92]
Value of j --> 3
Is 33 > 49
No Swap
Value of j --> 4
Is 49 > 23
Swap values
Current Sort Order:: [5, 9, 8, 33, 23, 49, 66, 69, 87, 92]
Value of i :: 4
Value of j --> 0
Is 5 > 9
No Swap
Value of j --> 1
Is 9 > 8
Swap values
Current Sort Order:: [5, 8, 9, 33, 23, 49, 66, 69, 87, 92]
Value of j --> 2
Is 9 > 33
No Swap
Value of j --> 3
Is 33 > 23
Swap values
Current Sort Order:: [5, 8, 9, 23, 33, 49, 66, 69, 87, 92]
Value of i :: 3
Value of j --> 0
Is 5 > 8
No Swap
Value of j --> 1
Is 8 > 9
No Swap
Value of j --> 2
Is 9 > 23
No Swap
Value of i :: 2
Value of j --> 0
Is 5 > 8
No Swap
Value of j --> 1
Is 8 > 9
No Swap
Value of i :: 1
Value of j --> 0
Is 5 > 8
No Swap
After Sort:: [5, 8, 9, 23, 33, 49, 66, 69, 87, 92]

 

 

Let's Share
Show Buttons
Hide Buttons