Algorithms are pretty important component in programming. In this tutorial we are going to learn about Bubble sort algorithm. This bubble sort algorithm is a basic one among the sorting algorithms. This article will explain about bubble sort algorithm, how it works and how to implement in in your code. If you are totally a newbie to Algorithms, I suggest you to start this tutorial series with this – “What is an Algorithm and why it is important?”
Sorting Algorithms:
Sorting algorithm is the type of algorithm which is used to sort the given data in desired order. It may be a sort in descending order or ascending order or maybe in alphabetical order. These algorithms will help the Programmer to sort the given data correctly. Out of all types of sorting Numerical and lexicographical sorting are quite famous sorting methods used by programmers.
Bubble sort:
In this type of sorting, data in the given array is sorted by making comparison with each other values. This means we will take two values in the array at a time and compare them. Then the values will be swapped or retained of their respective places in an array depending on the sort we need to achieve.
To explain this better, let’s consider the below array with five values and we need to sort this below array in ascending order.
To start of bubble sorting we need to compare the first two elements of the array [0] and [1].
Comparing 34 and 10, we knew 34 is greater than 10 and hence the position of these values are interchanged as shown below.
Now the next set of values in the array [1] and [2] ( 34 and 50 ) is compared and since 34 is lesser than 50 and they are retained in their respective positions.
Now the next set of values [2] and [3] (50 and 17) is compared with each other.
Since 17 is lesser than 50 their places in the array is swapped.
And now the final set of values [4] and [5] (50 and 5) is compared. Since 50 is greater than 5 their places in the array is swapped.
Now the final element [4] or 50 in the array is considered to be completely sorted and it is greater than all the elements in the array.
Next cycle:
Now the code returns to [0]th position of the array and again performs the comparison in pairs. Now last value (50) will be omitted from the comparison since it is considered to be fully sorted. This cycle repeats until the greater numbers are moved to the end of the array. Below you can find the resulting array at the end of each cycle. I have omitted the comparison step for the sake of simplicity. Last two values 34 and 50 is now considered to be fully sorted.
After the next cycle 17 is moved to [2] position in the array and now 17, 34 and 50 is considered to be fully sorted.
After the final cycle the entire array is fully sorted in ascending order and thus performing the task which we intend to perform.
Code:
#include <stdio.h> int array[5]={72,9,2,0,120}; //Data in Array int main() { int i,j; for(i=0;i<=4;i++) //Checking cycle { for(int j=0;j<=3;j++) { if(array[j]>array[j+1]) //Comparing the values { int temp=array[j+1]; array[j+1]=array[j]; array[j]=temp; } } } for(i=0;i<=4;i++) printf("%d\n",array[i]); //Printing sorted array return 0; }
This is just a sample code implementing the bubble sort algorithm. Of course it is not the only way to implement this sort and there are plenty of improvements to be made, but you have got the idea.
Activity:
In order to understand this algorithm better you need to practice implementing this in a code. Hence try to code the below exercise in any language of your preference.
- Write a program that allows user to enter the array values and then sort those values in an array in descending order.
Hope this tutorial helped you to understand bubble sort better. Do share the result of your exercise or queries or your feedback in the comments section below. See you soon with tutorial on another algorithm.