Bubble Sort in C
In this article we will discuss the Bubble Sort algorithm. This article will be very helpful and interesting as the working procedure of bubble sort is simplest.
Bubble Sort is a simplest sorting algorithm. It is a comparison based algorithm in which each pair of elements are compared and are swapped if they are not in order.
Working of Bubble Sort Algorithm
Now let' see the working of Bubble Sort Algorithm.
To understand the working let us take an unsorted array(unsorted array means the data present in the array is in jumbled order that is neither in ascending order nor descending)and start sorting it using Bubble Sort Algorithm.
Lets start by taking the array elements.
In figure 1 first element is not at its correct position so we have to sort it. Bubble Sort starts by comparing the first and second element (a[0] and a[1]). Let compare them which is greater and swap them.
Here in figure 2, a[0] > a[1] so we have to swap it.
Now in figure 3 we have to compare second and third element (a[1] and a[2]), since second element is greater than third element we have to swap it.
Now in figure 5 we have to compare third and forth element (a[2] and a[3]), since third element is samller than forth element it means they are in correct order no swapping is required.
Now in figure 6 we have to compare forth and fifth element (a[3] and a[4]), since forth element is greater than fifth element we have to swap it.
Now, we reach at the end of the array.
So this was our first pass i,e. full iteration of the array elements.
Our array is not sorted so same process will be followed for second iteration.
let's proceed
Now in figure 9 first and second element is already sorted likewise second and third element. Hence no need to swap them.
We will check the third and forth element , since third element is greater then the forth element. So swapping will take place.
Now if you look carefully in fig 11 you will find that all elements in the array are sorted and the forth and fifth element doesn't need to swap as it is already present in the right position.
Hence our array is sorted. So full process of sorting using Bubble sort is represented in the below figure.
Just have a look
As we have learned the working of Bubble Sort Lets proceed towards the Pseudo Code.
Pseudo Code Of Bubble Sort
We are moving towards the Algorithm of Bubble Sort.
Algorithm of Bubble Sort
Bubble Sort Program in C
Time Complexity
Now let's see the Time complexity of Insertion Sort in Best case, worst case, Average case. We will also be learning about space complexity.
Best Case: In Bubble Sort best case occur when the array is already sorted.
Worst Case: Worst Case occurs when we have to sort the array elements in ascending order, but its elements are in descending order.
Average Case: Average Case of Bubble Sort occurs when the element are in jumbled order i.e. neither in ascending nor in descending order.
Space Complexity
Advantage of Bubble Sort
Bubble Sort is simple and easy to implement
It does not require any extra memory
Minimal space requirement than that of other sorting algorithms
Disadvantage of Bubble Sort
Bubble Sort is code inefficient
It is only meant for academic purpose not for practical purpose
It does not work well when we have large unsorted list
When to use Bubble Sort
It is used when the array contains small number of elements.
When there are only few elements left to be sorted
Conclusion
So far our Bubble Sort article comes to an end but before ending we just conclude what are the things that we have learned today.
To conclude, it can be said that
Bubble Sort algorithm one of the simplest algorithm with simple implementation
It is efficient for small data values
Bubble Sort is a comparison based algorithm in which each pair of elements are compared and are swapped if they are not in order.