Bubble Sort Algorithm

Bubble Sort Algorithm

sorting by swapping

Hello Programmers ! This is Yuvraj here. I am a Computer Science Engineering Student. Today I am going to explain Bubble Sort Algorithm.

Before learning about Bubble Sort Algorithm, we need to be familiar with sorting. So sorting is basically arranging the data in Ascending, Descending, Alphabetically or any other particular format.

Example of sorting : sorting the products in shopping sites according to lowest price, sorting YouTube videos on the basis of newest or oldest etc.

Now we are familiar with sorting so lets move on to Bubble Sort.

In Bubble Sort, we do sorting by swapping the adjacent elements. Lets say we have 5 Numbers [12, 14, 13, 16, 10].

12.png

Now our Task is sorting these numbers in Ascending Order using Bubble Sort Algorithm(i.e 10, 12, 13, 14, 16).

So for sorting these elements we will check each and every element with its next adjacent element and if we find the next adjacent element is smaller than the previous element then we will swap those elements. We will repeat this process (n-1) times, here n is the number of elements in Array. Suppose if we have 5 elements then we will iterate 4 times as you can see in the image below.

}.png

The above process will be able to sort 1 element completely and the element will reach at the last index, we will do the same for (n-1) times.

Now lets see How we will write the code for it :

before writing code for Bubble Sort Algorithm, we need to have knowledge how we swap elements in programming.

int temp = arr[0];
arr[0] = arr[1];
arr[1] = temp;

Now we are ready to write program for Bubble Sort :

I am writing Bubble Sort code in Java, you can use any other programming language for the same.

/*We import this Scanner class to take inputs from the user*/
import java.util.Scanner;

public class BubbleSort{

    /*defining bubble sort method*/
    static void bubble_sort(int[] arr){
        /* temp variable for swapping elements */
        int temp;
        /*This for loop will execute (n-1) times 
          as we have discusses above */
        for(int i = 0; i<arr.length-1; i++){
            /*This for loop will execute (n-i) times 
              because after each iteration of outer loop 
              we will get a sorted element at the last */
            for(int j = 1; j<arr.length - i; j++){
               /*Here we will check the previous element (i.e. arr[j-1]) 
                 with the present element (i.e. arr[j]) and if we find previous 
                 element is bigger than the present element then we will 
                 swap those two elements with the help of temp variable. */
                if(arr[j-1]>arr[j]){
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
       /* creating object of scanner class */
        Scanner scr = new Scanner(System.in);
        System.out.print("Enter Size of Array. n = ");
        int n = scr.nextInt();
       /* creating array of n size to insert elements */
        int [] arr = new int[n];
       /* getting array elements from the user */
        for(int i = 0; i<n; i++){
            arr[i] = scr.nextInt();
        }

        /* printing the array elements before sorting */
        System.out.print("Elements in Arrays Before Sorting : ");
        for(int i = 0; i<n; i++){
            System.out.print(arr[i]+" ");
        }

       /* calling bubble_sort() method to sort the elements */
        bubble_sort(arr);

       /* printing the array elements after sorting */
        System.out.println();
        System.out.print("Elements in Arrays After Sorting : ");
        for(int i = 0; i<n; i++){
            System.out.print(arr[i]+" ");
        }
    }
}

I have tried my best to explain this bubble sort algorithm. I hope you have understood the concept.

Thank you

Connect with me on LinkedIn