C Programming

How to Implement Bubble Sort in C Programming?

Sorting arrays or containers is a common process in programming, and C++ offers various sorting algorithms to implement. Among them, Bubble Sort is the easiest and simplest algorithm to implement in C++.

This article discusses an easy way to implement Bubble Sort in C programming.

What is Bubble-Sort in C Programming?

In Bubble sort, the elements are repeatedly arranged in order, whether in ascending or descending order, depending on the user’s preference. The sorting process in C begins by searching the first index and comparing the first and second elements. If the first index element is greater than the second, they are swapped. The same comparison and swapping process is repeated for the second index element and the third element until all elements are sorted.

How Bubble Sorts Works?

Here is a step-by-step guide to implementing Bubble Sort in C.

Let’s consider the input array {5, 3, 1, 4, 6}. To sort this array using Bubble Sort, we follow the below passes:

First Pass:

(5 3 1 4 6) -> (3 5 1 4 6), Swap since 5 > 3

(3 5 1 4 6) -> (3 1 5 4 6), Swap since 5 > 1

(3 1 5 4 6) -> (3 1 4 5 6), Swap since 5 > 4

(3 1 4 5 6) -> (3 1 4 5 6), No swap as all elements are in order.

Second Pass:

(3 1 4 5 6) -> (1 3 4 5 6), Swap since 3 > 1

(1 3 4 5 6) -> (1 3 4 5 6), No swap as all elements are in order.

Third Pass:

(1 3 4 5 6) -> (1 3 4 5 6), No swap as all elements are in order.

The array is sorted, and the algorithm recognizes this after the third pass as there were no swaps.

Program of Bubble Sort in C

The following is the implementation of Bubble Sort in C programming.

#include <stdio.h>

int main () {
 
  int array[100], n, x, y, s;

printf("Please Enter the Number of array Elements: ");

scanf("%d", &n);

printf("Please Enter the Elements Values: ");

for(x = 0; x < n; x++)

scanf("%d", &array[x]);

for(x = 0; x < n - 1; x++) {

for(y = 0; y < n - x - 1; y++) {

    if(array[y] > array[y + 1]) {

      s = array[y];

      array[y] = array[y + 1];

array[y + 1] = s; }

    }

  }

printf("Sorted Array after using bubble sort: ");

for(x = 0; x < n; x++)

  {

printf("%d ", array[x]);

    }

return 0;

}

The above C program first initializes an array with a size of 100 elements and asks the user to enter the size of the elements that need to be sorted then entered elements from the user one by one. The entered values in an array are then sorted using nested loops as the code swaps the elements if they are in the wrong order. Finally, it prints the sorted array using for loop.

Output

Conclusion

Bubble sort is a sorting algorithm used to arrange the array in ascending or descending order by comparing each element of the array with its adjacent element and matching until the last element is sorted. In the above-mentioned guidelines, you will learn the basic of the Bubble sort algorithm and its implementation in C programming.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.