Bubble Sort program in C Language

Bubble Sorting algorithm implementation – Bubble sort is also known as sinking sort. This algorithm compares each pair of adjacent items and swaps them if they are in the wrong order, and this same process goes on until no swaps are needed. In the following program we are implementing bubble sort in C language.


In this program user would be asked to enter the number of elements along with the element values and then the programs would sort them in ascending order by using bubble sorting algorithm logic.

Time Complexity

  • Worst and Average Case: O(n*n).
  • Best Case Time Complexity: O(n).
  • Auxiliary Space: O(1)
  • Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted.
  • Sorting In Place: Yes
  • Stable: Yes
<img width=

Types of Sorting Algorithms:

Bubble Sort algorithm C Program implementation



#include<stdio.h>

int main(){

   int count, temp, i, j, number[30];

   printf("How many elements YOU Wants to enter?: ");
   scanf("%d",&count);

   printf("Enter %d numbers: ",count);

   for(i=0;i<count;i++)
   scanf("%d",&number[i]);

   /* This is the main logic of bubble sort algorithm 
    */
   for(i=count-2;i>=0;i--){
      for(j=0;j<=i;j++){
        if(number[j]>number[j+1]){
           temp=number[j];
           number[j]=number[j+1];
           number[j+1]=temp;
        }
      }
   }

   printf("Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}





OUTPUT



How many elements YOU Wants to enter?: 5

Enter 5 Elements : 11,8,9,4,3

Order of Sorted elements is: 3,4,8,9,11

Additional Reading

you can read more articles like this here.

READ MORE

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.❤

Leave a Reply