Bubble Sort in C

Bubble Sort in C
Satyam Chaudhary
Data Structure Jun 08, 2023

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.

fig 1:Example of an array

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.

fig 2:Swapping a[0] and a[1]

Here in figure 2, a[0] > a[1] so we have to swap it.

fig 3:After Swapping

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.

fig 4:Swapping a[1] and a[2]
fig 5:After Swapping

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.

fig 6:No Swapping

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.

fig 7:Swapping a[3] and a[4]
fig 8:After Swapping

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

fig 9:After First Pass

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.

fig 10:Swapping a[3] and a[4]
fig 11:After Swapping

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

fig 12:Final Array after sorting using Bubble Sort.

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

Fig 13: Output

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.

Fig 14: Time Complexity of Bubble Sort

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

Fig 15: Space Complexity of Bubble Sort

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.

sorting method
bubble sort in c
bubble sort in data structure
bubble sort program

Satyam Chaudhary


Satyam is a brilliant Engineering Undergraduate and proud Indian. He is passoinate towards web development and acquiring new skills.

He help students in understanding the concepts with a correct approach and solve their academic problems.

We are here to clear your doubts with an easy step by step approach.