Popular

Data Science

Technology

Finance

Management

Future Tech

Internship Assurance

DevOps & Cloud Engineering

In this article, we will learn about the bubble sort algorithm and see how to implement it step by step in the C programming language. Bubble sort is a foundational algorithm that sorts by continuously swapping neighbouring elements until the entire array is in order. We’ll guide you through a detailed implementation of the Bubble Sort algorithm using the C programming language, ensuring you understand each step and can confidently apply this knowledge in your own projects.

Bubble sort is the simplest sorting algorithm that repeatedly steps through the list or array data structure through the list. It compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Let’s see how the bubble sorting algorithm works:

- Run the two loops nested in one

another. - The outer loop will run from i = 0 to i

< n -1, where n is the number of elements in the list. - There is one more loop, which is run on

the inner loop j=0 to j < n-i -1. It is because, after each iteration of the outer loop, one element at

the end (or at the start if the order is decreasing order) will be in its right place, so we can leave it as

is. - Then, we will check if the arr[j] >

arr[j+1]]. If it’s true, then we will swap places of these elements. If this condition is false, we will

continue to the next iteration. - This process will be repeated till the

conditions of the loop are not satisfied.

For Decreasing, we have to change this thing in our bubble sort algorithm.

- The inner loop will run from j = i to j

< n -1. - We will compare the elements as arr[j]

< arr[j+1].

The following program demonstrates the Bubble Sort Program in C language:

**Program**

**Output**

**Complexity Analysis of Bubble Sort:**

**Time Complexity:** O(N2), where N is the number of items in the list.

**Auxiliary Space:** O(1)

In this section, we will see the Bubble Sort algorithm. Let’s take an unsorted array and sort it using the Bubble Sort Array.

Let’s take the example of an array.

13 | 32 | 26 | 35 | 10 |

In the First iteration, we will start with the initial two elements. We will compare them to check whether the first element is greater or not.

13 |
32 |
26 | 35 | 10 |

In this array, 32 is greater than the first element of the array, which is 13. So it is already sorted. We don’t need to sort them.

13 | 32 |
26 |
35 | 10 |

Here, 26 is smaller than 32. So, we will swap. After swapping new array will look like

13 | 26 | 32 | 35 | 19 |

Now, we compare 32 and 35

13 | 26 | 32 | 35 |
10 |

Here, 35 is greater than 32. So, no swapping is required in this new sorted array. Let’s compare it to between 35 and 10.

13 | 26 | 32 | 35 | 10 |

Let’s move the second interaction in this array.

The same process will be followed in the second phase.

13 |
26 |
32 | 10 | 35 |

13 | 26 |
32 |
10 | 32 |

13 | 26 | 32 |
10 |
35 |

Here, we see that 10 is smaller than 32. So, swapping will be performed. After swapping, the array will be:

13 | 26 | 10 | 32 | 35 |

13 | 26 | 10 | 32 |
35 |

Let’s move the third iteration in the loop.

The same process will be followed for the third iteration in this section.

13 |
26 |
10 | 32 | 35 |

13 | 26 |
10 |
32 | 35 |

10 is smaller than 26, so I will swap them. After swapping, the array will look like this.

13 | 10 | 26 | 32 | 35 |

13 | 10 | 26 |
32 |
35 |

13 | 10 | 26 | 32 |
35 |

Now. Let’s move to the fourth iteration.

The fourth iteration. The array will look like this

10 | 13 | 26 | 32 | 35 |

Here, we don’t need to swap any element. This array is now sorted.

In this section, we will create a bubble sort program using the for loop. We have declared and initialised an array of size 5 with values such as 44, 33, 11, 22, 55, etc. For sorting, we used nested loops and kept checking on adjacent elements of a one-dimensional array.

The following program demonstrates the for loop:

**Program**

Sorted array:

Internship Assurance

DevOps & Cloud Engineering

In this section, we will create a bubble Sort program using the while loop in C language. We will now use a while loop to sort the array in ascending order using the Bubble Sort Algorithm.

The following program demonstrates the while loop

**Program**

**Output**

In this section, we will implement the Bubble Sort algorithm using functions. The code of bubble sort is in user-defined functions, which contain the main mechanism.

The following program demonstrates the Bubble Sort in C language.

**Program**

**Output**

In this section, we will implement the Bubble Sort in C language using C pointers. We will use pointers and swap both values from their respective memory locations. In this way, we will be able to sort the array by constantly checking the adjacent elements.

The following program demonstrates the bubble sort in C using pointers

**Program**

**Output**

In this section, we will optimise the previous bubble sort code. If the array is sorted after some passes, it continuously checks (n-1) times, which is not an optimised way of executing an algorithm.

We can optimise the bubble sort algorithm using an additional variable named flag. The flag variable is set to true whenever elements are swapped, reducing the execution time. This is an optimised approach to implementing Bubble sort in the C language.

The following program demonstrates the Bubble Sort in C language:

**Program**

**Output**

In this article, we learned about the bubble sort algorithm. It is the simplest algorithm for sorting a list or array. However, it is not very efficient for large data structures. Its complexity is O(n2), meaning its performance decreases drastically as the size of the data increases. However, the bubble sort algorithm can still be useful for educational purposes or for sorting small datasets where simplicity is valued over efficiency.

FAQs

What is the Bubble Sort algorithm?

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until our list or array is sorted.

How does bubble sort work?

The bubble sorting algorithm working is very simple. It compares the adjacent elements in the list and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted, with the largest elements “bubbling” to the top.

Can bubble sort be optimised?

Yes, Bubble sort can be optimised by adding a flag to indicate whether any swaps were made during a pass through the list. If no swaps are made, the list is already sorted, and the algorithm will be terminated early.

Is bubble sort stable?

Yes, Bubble sort is a stable algorithm. It preserves the relative order of equal elements in the sorted list. If two elements have the same value, their relative order will be the same in the sorted list as it was in the original list.

Book a free counselling session

Get a personalized career roadmap

Get tailored program recommendations

Explore industry trends and job opportunities

Programs tailored for your success

Popular

Data Science

Technology

Finance

Management

Future Tech

Upskill with expert articles

View all

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved