Understanding Bubble Sorting Algorithm Using Pseudo Codes And Example In Java

Sorting is the technique to arrange data in ascending or descending order. There are different types of sorting algorithms that arrange data in particular format, all can be differentiate by their efficiency level. 

Bubble Sort -

This is very first and basic sorting algorithm where each two elements compares with each other and swap their position in array.

This algorithm takes lots of comparisons so that its complexity is O(n2)  where n is number of data values being sorted. 

Usually we don’t prefer such kind of sorting with large amount of data.

     Let's take an example, assuming I have list of 5 data values.
20, 55, 2, 71, 10

     As list containing 5 elements that means sorting process will be done in 5 cycles.

In every cycle, each elements will compare itself with it's previous element, If first element is greater than 2nd element then they will swap their positions in array so that small element will come first.



(i)     1st Cycle

Compare first two elements (array index 0 and 1). 

20 compares with 55. 

20 < 55 here swapping is not required.

Compare next two elements (array index 1 and 2). 

55 compares with 2. 

55 > 2 small element 2 will swap its position with 55’s position in array.

List would be like – 20, 2, 55, 71, 10

Compare next two elements (array index 2 and 3). 

55 compares with 71. 

55 < 71 here swapping is not required.

Compare next two elements (array index 3 and 4).

71 compares with 10.

71 > 10 i.e. small element 10 will swap its position with 71’s position in array.

List would be like – 20, 2, 55, 10, 71

Bubble Sorting Part I.
Bubble Sorting Algorithm Part-1. ByTechAchievers.com

(ii)    2nd Cycle


Compare each two elements (array index 0 and 1).

20 compares with 2.

20 > 2 i.e. small element 2 will swap its position with 20’s position in array.

List would be like – 2, 20, 55, 10, 71

Compare next two elements (array index 1 and 2).

20 compares with 55.

20 < 55 here swapping is not required.

Compare next two elements (array index 2 and 3). 

55 compares with 10.

55 > 10 i.e. small element 10 will swap its position with 55’s position in array.

List would be like – 2, 20, 10, 55, 71


Compare next two elements (array index 3 and 4).

55 compares with 71

55 < 71 here swapping is not required.
Bubble Sorting Algorithm
Bubble Sorting Algorithm Part-2. ByTechAchievers.com

(iii)    3rd Cycle

Compare each two elements (array index 0 and 1).

2 compares with 20.

2 < 20 here swapping not required.

Compare next two elements (array index 1 and 2).

20 compares with 10.

20 > 10 i.e. small element 10 will swap its position with 20’s position in array.

List would be like – 2, 10, 20, 55, 71


Compare next two elements (array index 2 and 3).

20 compares with 55.

20 < 55 here swapping not required.

Compare next two elements (array index 3 and 4).

55 compares with 71.

55 < 71 here swapping is not required.
Bubble Sorting Algorithm
Bubble Sorting Algorithm Part-3. ByTechAchievers.com

Same process will be done with 4th and 5th cycle as well, here you can see that list is already in sorted order, through programming we can control over useless cycles. 


   As you can see It takes too many comparisons that is why programmers don't prefer it for large amount of data. 


  Bubble Sort Implementation In Java –



public class BubbleSortingExample {



  public static void main(String []args){

   int size = 5;

   int temp =0;

   boolean isSortingDone = false;

   int arrayList[] = {20,55,2,71,10};

       
   for (int i = 0; i<size; i++) {

   
    isSortingDone = false;

    System.out.println( i+1 +" - cycle list ");

    printList(arrayList,size);
            

    for (int j=0; j<size-1; j++) {               

     System.out.println("Comparing "+arrayList[j]+" and "+
     arrayList[j+1]+"\n");
                

     if (arrayList [j] > arrayList [j+1]) {
                   

      // swapping process

      temp = arrayList[j];

      arrayList[j] = arrayList[j+1];                

      arrayList[j+1] = temp;
                   

      isSortingDone = true;                

    }

    else {

     System.out.println( "Swapping not required.\n ");

    }

    System.out.println( "Updated list ");

    printList(arrayList,size);

  }

  System.out.println ("-------------------------\n");
      
          
  if (isSortingDone == false ) {


    System.out.println("List has been  sorted in "+ i +" cycles");                    

    break;

  }           


 }


} // end of main method

    
 public static void printList (int arrayList[], int size) {


 for (int k =0; k<size; k++) {

   System.out.print (arrayList[k] +"  ");                   

 }

 System.out.println("\n");
      

 }// end of print method


} // end of class




 OUTPUT - 

1 - cycle list

20  55  2  71  10 


Comparing 20 and 55

Swapping not required.

Updated list

20  55  2  71  10 


Comparing 55 and 2

Updated list

20  2  55  71  10 


Comparing 55 and 71

Swapping not required.

Updated list

20  2  55  71  10 


Comparing 71 and 10

Updated list

20  2  55  10  71 


2 - cycle list

20  2  55  10  71 


Comparing 20 and 2

Updated list

2  20  55  10  71 


Comparing 20 and 55

Swapping not required.

Updated list

2  20  55  10  71 


Comparing 55 and 10

Updated list

2  20  10  55  71 


Comparing 55 and 71

Swapping not required.

Updated list

2  20  10  55  71 


3 - cycle list

2  20  10  55  71 


Comparing 2 and 20

Swapping not required.

Updated list

2  20  10  55  71 


Comparing 20 and 10

Updated list

2  10  20  55  71 


Comparing 20 and 55

Swapping not required.

Updated list

2  10  20  55  71 


Comparing 55 and 71

Swapping not required.

Updated list

2  10  20  55  71 


4 - cycle list

2  10  20  55  71 


Comparing 2 and 10

Swapping not required.

Updated list

2  10  20  55  71 


Comparing 10 and 20

Swapping not required.

Updated list

2  10  20  55  71 


Comparing 20 and 55

Swapping not required.

Updated list

2  10  20  55  71 


Comparing 55 and 71

Swapping not required.

Updated list

2  10  20  55  71 


As you can see - List has been sorted in 3 cycles.

No comments

Powered by Blogger.