C Program to Sort Array Elements Using Bubble Sort Algorithm - C Program

C Program to Sort Array Elements Using Bubble Sort Algorithm

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

#include <stdio.h>
void main()
{
 int num[5],size=5,i,j,temp;
 
 printf("enter %d elements of array : ",size);
 for(i=0;i<size;i++) 
     scanf("%d",&num[i]);
 for(i=1;i<size;i++)
 {
	for(j=0;j<size-i;j++)
	{
		if(num[j]>num[j+1])
		{
			temp=num[j];
			num[j]=num[j+1];
			num[j+1]=temp;
		}
	}
 }

 printf("Array Element After Sorting....\n");
 for(i=0;i<size;i++)
	printf("%d ",num[i]);

 return 0;
}

Output - C Program to Sort Array Elements Using Bubble Sort Algorithm

Enter 5 elements of array : 3 5 1 6 2   
Array Element After Sorting....     
1 2 3 5 6                          
           
Compile and Run Online