Table of Contents
It is the basic sorting algorithm to sort array elements. It compares the pair of adjacent elements from an array. If the order is wrong, it just swaps the element.
If there are n-elements in the array, it iterates loop n times. In each iteration, one element is placed in the order at the end of the array.
It is not easy to understand this algorithm with words. So, in this post, I will ponder Bubble sort in C with the explanation in detail. It will make your understanding clear.
Here is an algorithm for Bubble Sort.
Let’s take an array of elements 52, 35, 91, 31, 56
Here we want to order it in ascending order.
Iteration 1:
Keep a pointer to the first element (5) and compare it with 35. As 35 is lower than 52, swap it.
Result array: 35, 52, 91, 31, 56
Increment the pointer to point to the second element i.e. 52. Compare it with the 3rd element (91). As 91 is greater than 52, do nothing.
Increment the pointer to point 3rd element (91) compare with the 4th element (31). As 91 is greater than 31, just swap them.
Result array: 35, 52, 31, 91, 56
Again increment the pointer and compare 91 with 56. As 91 is greater than 56, swap them.
Result array: 35, 52, 31, 56, 91
You can see, the largest element has reached the last position.
Iteration 2:
After the second iteration,
Result array: 35, 31, 52, 56, 91
The array is partitioned into two parts; the second half is sorted (mentioned above in bold format).
Note: To optimize the algorithm, you can skip comparing elements with the elements from the second half.
Iteration 3:
Result array: 35, 31, 52, 56, 91
Iterate the above steps (n-1) times (until you get a sorted array).
Iteration 4:
Final sorted array: 31, 35, 52, 56, 91
Here is a complete code for bubble sort.
#include<stdio.h> void main() { int nArr[]={34, 30, 12, 92, 47}; int nSize = sizeof(nArr)/sizeof(int); int i=0, j=0, t=0; //Special Case to reduce complexity in Best Case(O(n)) int nSorted= 0; printf("\nInput array: "); for(i=0;i<nSize;i++) { printf("%d ", nArr[i]); } for(i = nSize-1; i>0 && nSorted == 0; i--) { nSorted = 1; for(j=0;j<i;j++) { if(nArr[j]>nArr[j+1]) { t= nArr[j]; nArr[j]=nArr[j+1]; nArr[j+1] = t; nSorted = 0; } } } printf("\nSorted array: "); for(i=0;i<nSize;i++) printf("%d ", nArr[i]); }
The output of a program:
Input array: 34 30 12 92 47 Sorted array: 12 30 34 47 92
Loops run twice for each element in an array so it takes time O(n^2).
Let’s take the best case where the input array is already sorted. There is no need for swapping elements and only one iteration is required. So the best case complexity is Ω(n).
In average and worst cases, it compares each element to its adjacent element in the array. It requires n iterations. So the average case and worst case complexity are O(n^2).
Worst Case Complexity: O(n^2) Best Case Complexity: Ω(n) Average Case Complexity: O(n^2)
What are the advantages of using Bubble sort?
What are the disadvantages of using Bubble sort?
Other Sorting Algorithm:
This is all about bubble sort in C with explanation. If you have any questions, please write in a comment.
Hello, I to am Python nut. I very much appreciate as you share your coding knowledge. Keep up.
You motivate me to learn and share more with you. Thanks, Jacob!