Sort array in C using bubble sort algorithm

Write a c program to sort a given array using the bubble sort algorithm. In this article, we are going to print the given array after sorting it using the bubble sort algorithm

What is the Bubble sorting

Bubble sorting is a simple technique in which we access elements one by and compare them with element one index forward to it and the swapping the elements and then doing so and so we got our sorted array

Algorithm for bubble sort in C

  • Import library, take input of array
  • Running the loop to access element
  • Checking the condition and swap the elements
  • Printing the sorted array

Sorting array in C using the Bubble sort algorithm

void main(){
    int i,j,t,n,a[100];
    printf("enter the size:");
    printf("enter the element");
            if (a[j]<a[i]){
                a[j]=t	;			
    printf("sorted array \n");
    printf("%d ",a[i]);

Explanation of the code

Step 1 includes the library

Step 2 declare the main function using the void as its return type

Step 3 declaring the variables and array

Step 4  command to the user to give the size of the array

Step 5 scanf to take the input

Step 6 command to the user to give input of element

Step 7 running for loop to take input again and again

Step 8 scanf to take input

Step 9 closing the scope

Step 10 for loop to access the element one by one

Step 11 for loop to access  the element access next element with the before one

Step 12  checking for the condition that is which element is greater than other

Step 13, Step 14 and Step 15  if it satisfies the condition then swap the elements

Step 16, Step 17 and Step 18  closing the scopes

Step 19 message to the user

Step 20 running the for loop again to print the array with is sorted

Step 21 printing the array

Closing the scopes


In this blog, we had learned how to sort an array using the bubble sort algorithm