Bubble sort belongs to a family of sorting algorithms. It is a sequential sort algorithm and an iterative approach to sorting the array or a list. Same is used for sorting the array or a list in an ascending or descending order as per the problem’s requirement. Here arises a question, why Bubble Sort is named as “Bubble”?

Now coming to answer, we all have seen air bubbles in water and how they lift up from bottom to air. There is science behind it. Bubbles have less density than water and that’s why they rush from the bottom surface to the top due to high pressure around it. This phenomenon is called Buoyancy.

Hence, air bubbles that are more buoyant than surrounding water rise to the top.

Similarly, bubble sort is worked upon the logic of lifting up the air bubble from the surrounding.

If we have unsorted elements in an array or in a list and we have to sort it in an ascending order, then we can use the bubble sort for sorting of elements. If an array contains a lesser value element then it gets lifted up and takes the position of greater one and greater will take the lesser one’s position. Also, we can say that the greater value will swap with the lesser value and the process is going on repeat again and again until the whole array or a list is not sorted.

We know that bubble sort is an iterative process, so it performs the iteration till (n-1) elements in the array to sort it.

Let’s consider an example of an unsorted array which contains these elements:

7,9,6,8,2.

Step 1: Bubble sort starts sorting from the first element of an array, comparing the first 2 elements to check which one is greater.

In a given array first two elements are 7 and 9 means 7 is less than 9 then the array remains same. Hence no changes occur.

 7
 9
 8
 6
 2

Step 2: In this case, we find that 9 is greater than 8 then these two values must be swapped.

The new array should look like this:

 7
 8
 9
 6
 2

Step 3: Next we compare 9 and 6. 9 is greater than 6, so they must be swapped.

 7
 8
 6
 9
 2

Step 4: Next we have 9 and 2. 9 is greater than 2, so they must be swapped.

 7
 8
 6
 2
 9

Here, we can see that greater number in an array i.e. 9 occupy its right position.

By following the same steps, we will sort the array till n-1 element.

The resultant array should look like this:

 2
 6
 7
 8
 9

Interesting to see that how lesser value element lifted up and take the position of the greater value element with the help of swapping.

Now, here is the source code of Bubble Sort  −

#include <stdio.h>

int main()

{

int array, n, c, d, swap;

printf(“Enter number of elements\n“);

scanf(“%d”, &n);

printf(“Enter %d integers\n“, n);

for (c = 0; c < n; c++)

scanf(“%d”, &array[c]);

for (c = 0 ; c < ( n – 1 ); c++)

{  for (d = 0 ; d < n – c – 1; d++)

{  if (array[d] > array[d+1]) /* For ascending order use < */

{

swap       = array[d];

array[d]   = array[d+1];

array[d+1] = swap;

}

}

}

printf(“Sorted list in ascending order:\n“);

READ  Best 5 YouTube Channels for Machine Learning, Artificial Intelligence and Data Science

for ( c = 0 ; c < n ; c++ )

printf(“%d\n“, array[c]);

return 0;

}