Home » C Tutorial » What is bubble sort in C?

What is bubble sort in C?

The bubble sort in c is a sorting algorithm used for sorting. Sorting is the process of arranging the data in ascending order or descending order. Many sorting techniques like insertion sort, merge sort, heap sort, quick sort, etc. But the most simple and easy way to sort the data is bubble sort. Bubble sort is easy to understand and simple to implement into the code. 

The bubble sort algorithm uses a compare and swap technique. In bubble sort, we compare the adjacent elements i.e., the contiguous elements. If the left element is greater than the right, then swap them. If not, keep it as it is. 

In bubble sort, we get the first highest element in the first iteration, the second-highest element in the second iteration, and so on.

The time complexity for a bubble sort is the same for best and worst-case. For the first iteration, we compare (n-1) times. For the second iteration, we compone (n-2) times and so on. 

Therefore, total no. of comparison = (n-1) + (n-2) + (n-3) + … +1 

so the time complexity is O(n^2).

Bubble sort algorithm in C.

Step 1 – Start.

Step 2 – Find the count of items in a data set.

Step 3 – If the left element is greater than the right element, then swap otherwise, check for the next two elements.

Step 4 – Perform step 3 for (count – 1) times.

Step 5 – stop.

Advantages of bubble sort.

  1. C makes bubblesort very easy to understand and simple to write.
  2. Bubble sort has very few lines of code.
  3. A sorted array performs greatly and efficiently.
  4. There is no need for external memory to store temporary data.
  5. Bubble sort provides excellent stability.

Disadvantages of bubble sort.

  1. Bubble sort is not useful for large data sets.
  2. Bubble sort takes a lot of time to execute. That’s why it is not applicable while reversing the data set.
  3. At the time of the best-case bubble sort compares (n-1) time, which is unnecessary.
  4. Bubble sort is the slowest algorithm as compared to other algorithms.
Bubble sort in C

Examples of bubble sort in C program.

There are various ways to use the technique of bubble sorting in C. The logic and the algorithm will be the same for all the examples. As we know, bubble sort is the slowest algorithm ever. To make it more efficient, we use different ways like using pointers to share the same memory, Functions to use the algorithm multiple times, etc. Let’s understand all the codes deeply.

1. Bubble sort program in C using for loop.

Using for loop is the most optimized program of bubble sort. We have set the condition for the inner for loop (n-i-1), which will avoid the extra iteration. Because of this, the execution time will become less.

Example:

#include <stdio.h>
int main()
{
    int a[100], n, i, j, temp;
    
    printf("\nEnter the number of elements:");
    scanf("%d", &n);
    
    printf("\nEnter the Elements:");
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < n-1; i++)
    {
        for(j = 0; j < n-i-1; j++)
        {
            if(a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    printf("\nSorted data is : ");
    for(i = 0; i < n; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}

Output

Enter the number of elements:10

Enter the Elements:45
982
32
63
06
32
64
1423
097
543

 Sorted data is :  6     32      32      45      63      64      97      543     982     1423

2. Bubble sort program in c using while loop.

We can also use the while loop for the bubble sort program. We use the same logic for this code with using a while-loop for the outer loop.

Example:

#include <stdio.h>
int main()
{
    int a[100], n, i, j, temp;
    
    printf("\n Enter the number of elements:");
    scanf("%d", &n);
    
    printf("\n Enter the Elements:");
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);

    i = 0;
    while(i < n - 1) {
        j = 0;
        while(j < n - i - 1) {
            if(a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
            j++;
        }
        i++;
    }
    
    printf("\nSorted data is : ");
    for(i = 0; i < n; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}

Output

Enter the number of elements:10

Enter the Elements:97
24
46
57
78
32
45
68
23
35

 Sorted data is :  23    24      32      35      45      46      57      68      78      97

3. Bubble sort program in c using user-defined function.

The user-defined function will help to use the bubble sort program in c multiple times. Pass the data entered by users to the function and write the function for the bubble sorting.

Example:

#include <stdio.h>
void bubble_sort(int a[100],int n)
{
	int i, j, temp;
	for(i = 0; i < n-1; i++)
    {
        for(j = 0; j < n-i-1; j++)
        {
            if(a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    printf("\nSorted data is : ");
    for(i = 0; i < n; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
}
int main()
{
    int a[100], n, i;
    
    printf("\nEnter the number of elements:");
    scanf("%d", &n);
    
    printf("\nEnter the Elements:");
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);

    bubble_sort(a,n);
    return 0;
}

Output

Enter the number of elements:10

Enter the Elements:35
98
20
58
19
8
50
35
12
43

Sorted data is :  8     12      19      20      35      35      43      50      58      98

4. Bubble sort program in c using recursion.

To avoid the use of loops, we can use the recursive functions for the bubble sort program in c. This method also takes more time to execute compared with the method of for loop.

Example:

#include<stdio.h>
void BubbleSortRecursion(int a[],int n)
{
 int i,j,tmp;
 i=n;
if(i>0)
  {
       for(j=0;j<n-1;j++)
       {
         if(a[j]>a[j+1])
          {
            tmp=a[j];
            a[j]=a[j+1];
            a[j+1]=tmp;
          }
      }
  BubbleSortRecursion(a,n-1);
  }
else
  {
       return;
   }
}
int main()
{
	int a[100], i,j,num,temp;
	printf("Enter number of elements:");
	scanf("%d",&num);	
	printf("Enter numbers:");
	for(i=0;i<num;i++)
	{
 		scanf("%d",&a[i]);
	}
	BubbleSortRecursion(a,num);
	printf("Sorted data is : \n");
	for(i=0;i<num;i++)
	{
 		printf("%d\t",a[i]);
	}
	return 0;
}

Output

Enter number of elements:10
Enter numbers:50
40
30
70
60
90
80
100
20
10
Sorted data is :
10	20	30	40 	50	60	70	80	90	100

5. Bubble sort program in c using pointers.

Using a pointer in the bubble sort will help to share the same memory in the function. This method executes faster than the method of recursion.

Example:

#include <stdio.h>
void Swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}
void bubbleSort(int a[], int n) {
    int i, j, temp;
    for(i = 0; i < n - 1; i++) {
        for(j = 0; j < n - i - 1; j++) {
            if(a[j] > a[j + 1]) {
                Swap(&a[j], &a[j + 1]);
            }
        }
    }
}
int main()
{
    int arr[100], n, i;
    
    printf("\nEnter the total Number of Elements:");
    scanf("%d", &n);
    
    printf("\nEnter the Array Elements  :  ");
    for(i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
    bubbleSort(arr, n);
    printf("\nSorted data is: ");
    for(i = 0; i < n; i++)
    {
        printf(" %d \t", arr[i]);
    }
    printf("\n");
    return 0;
}

Output

Enter the total Number of Elements:10

Enter the Array Elements  :  48
39
26
58
29
10
5
3
25
64

Sorted data is:  3       5       10      25      26      29      39      48      58      64

Pin It on Pinterest