Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.

How Bubble Sort Works?

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise.

14 33 27 35 10

Bubble sort starts with very first two elements, comparing them to check which one is greater.

14 33 27 35 10

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.

14 33 27 35 10

We find that 27 is smaller than 33 and these two values must be swapped.

14 33 27 35 10

The new array should look like this −

14 27 33 35 10

Next we compare 33 and 35. We find that both are in already sorted positions.

14 27 33 35 10

Then we move to the next two values, 35 and 10.

14 27 33 35 10

We know then that 10 is smaller 35. Hence they are not sorted.

14 27 33 35 10

We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this 

14 27 33 10 35

To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this −

14 27 10 33 35

Notice that after each iteration, at least one value moves at the end.

14 10 27 33 35

And when there's no swap required, bubble sorts learns that an array is completely sorted.

10 14 27 33 35

Now we should look into some practical aspects of bubble sort.

Algorithm

We assume list is an array of n elements. We further assume that swap function swaps the values of the given array elements.

begin BubbleSort(list)

   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
   
   return list
   
end BubbleSort

 

Program

  1. public class BubbleSortExample {  
  2.     static void bubbleSort(int[] arr) {  
  3.         int n = arr.length;  
  4.         int temp = 0;  
  5.          for(int i=0; i < n; i++){  
  6.                  for(int j=1; j < (n-i); j++){  
  7.                           if(arr[j-1] > arr[j]){  
  8.                                  //swap elements  
  9.                                  temp = arr[j-1];  
  10.                                  arr[j-1] = arr[j];  
  11.                                  arr[j] = temp;  
  12.                          }  
  13.                           
  14.                  }  
  15.          }  
  16.   
  17.     }  
  18.     public static void main(String[] args) {  
  19.                 int arr[] ={3,60,35,2,45,320,5};  
  20.                  
  21.                 System.out.println("Array Before Bubble Sort");  
  22.                 for(int i=0; i < arr.length; i++){  
  23.                         System.out.print(arr[i] + " ");  
  24.                 }  
  25.                 System.out.println();  
  26.                   
  27.                 bubbleSort(arr);//sorting array elements using bubble sort  
  28.                  
  29.                 System.out.println("Array After Bubble Sort");  
  30.                 for(int i=0; i < arr.length; i++){  
  31.                         System.out.print(arr[i] + " ");  
  32.                 }  
  33.    
  34.         }  
  35. }  

Output:

Array Before Bubble Sort
3 60 35 2 45 320 5 
Array After Bubble Sort
2 3 5 35 45 60 320 

Related Articles

post a comment