Updated April 11, 2023
Introduction to Bubble Sort in C
In C programming language there are different sorting techniques such as selection sort, bubble sort, merge sort, quick sort, heap sort, insertion sort, etc. Sorting is a process of arranging elements or items or data in a particular order which is easily understandable to analyze or visualize. In this article let us discuss on bubble sort. In C programming language, bubble sort is a simple technique of sorting that swaps or arranges the elements in ascending or descending order if the elements are not in correct order to swap the adjacent elements until the elements are arranged in the correct order.
Working of Bubble Sort
In general, bubble sort is also known as a sinking sort where each adjacent element is checked and swap if there are not in the correct order and this swapping of elements process is continued until there are no items or elements are left for swapping. Let us see the algorithm:
Algorithm
Let us consider a list of elements.
Step 1: In the first step it will start with the first element of the list and starts comparing it with the next element.
Step 2: This checking and swapping of the elements are done on the entire list. So to do this first element is compared with the adjacent element. That can be done using for loop
For all elements of the list
if list[ item at index 1] > list [item of index + 1]
Step 3: After comparing the elements swap these in ascending order using the below swap (list[item at index 1 ], list [item of index + 1])
Step 4: After swapping all the elements then these are arranged in ascending order.
Example of Bubble Sort in C
Let us consider an example below for sorting the list 46, 43, 52, 21, 33, 22, 89 using bubble sort.
#include <stdio.h>
void swap_ele(int *p, int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
void bubble_Sort(int a[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1])
swap_ele(&a[j], &a[j+1]);
}
void print_list(int a[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = {46, 43, 52, 21, 33, 22, 89};
int n = sizeof(a)/sizeof(a[0]);
bubble_Sort(a, n);
printf("Sorted list using bubble sort: \n");
print_list(a, n);
return 0;
}
Output:
In the above code, we have written 3 different functions each of which work differently firstly, we have written a function for swapping the numbers “swap_ele” is the function in which we take two variables by passing them as a parameter where one variable store the first element and the second variable stores the second element and inside the function we use “temp” variable where we use to store the value and swap the elements.
In the second function, it is a very important function which has the logic of working of bubble sort using the “swap_ele” function. In this “bubble_Sort” function we declare two variables “ i ” and “ j ”, where if we the value of i = 0 then the j loop points to the largest element of the list and after incrementing “i” value by 1 where i = 1, then “ j” loop points to the second largest element of the list and so on. Then using “for” loop we can traverse the list and then using “ if ” loop we are comparing the values stored in the two variables among which element is smaller is swapped to left and the bigger value elements get swapped to the right-hand side of the list using “swap_ele” function.
The third function is to just print the sorted list using the “for” loop and arrange in ascending order. Then lastly to compile the program we need to write the main function in which we declare a list and use the “bubble_sort” function on that list and print the sorted list using the “print_list” function.
So in general, the bubble sort working is very simple, where it will traverse the entire list and the largest item of the list will be swapped with all the items in the list and this largest element is placed at the extreme right of the list. So when the largest element is placed at the right position in the list now that element is not considered while comparing now again the second largest element is compared to all the leftover elements excluding the largest element also this second largest element is also placed at the rightmost side of the list that is just before the largest element and this process is continued until the there are no largest elements are left to swap and all the elements are sorted in the ascending order. Similarly, this can be done for sorting the list in descending order where there will be a change in the “ if ” loop in the “ for ” loop of function that contains the logic of bubble sort.
Conclusion
Bubble sort is one of the simplest sorting techniques, which is also known as a sinking sort. This sorting technique is usually used when introducing the sorting concept. Bubble sort is a technique of sorting or arranging the items of the list or array in either ascending order or descending order. This technique first determines the largest element in the list or array and then compares it with other items and then it is placed in the extreme eight of the list and this process is continued until there are no items are there to swap with.
Recommended Articles
This is a guide to Bubble Sort in C. Here we discuss the Working of Bubble Sort along with the Example and Algorithm with steps. You may also have a look at the following articles to learn more –