In this tutorial, I am going to show how to implement the Bubble Sort Program in C Language.
Bubble Sort Program in C:
As we all know that Bubble Sort is a most commonly used and easiest sorting algorithm among all sorting techniques. Bubble sort compares each element with its adjacent element and exchange (swap) them if they are not in order.
Input : 8 4 1 6 5
Output :
PASS 1 - :
Elements are in phase - 1 : 8 4 1 6 5
Comparing 8 and 4 : Since 8 > 4, Exchange (Swap) them
Elements are in phase - 1 : 4 8 1 6 5
Comparing 8 and 1 : Since 8 > 1, Exchange (Swap) them
Elements are in phase - 1 : 4 1 8 6 5
Comparing 8 and 6 : Since 8 > 6, Exchange (Swap) them
Elements are in phase - 1 : 4 1 6 8 5
Comparing 8 and 5 : Since 8 > 5, Exchange (Swap) them
After Pass 1 elements are :
4 1 6 5 ___8
PASS 2 - :
Elements are in phase - 2 : 4 1 6 5 ___8
Comparing 4 and 1 : Since 4 > 1, Exchange (Swap) them
Elements are in phase - 2 : 1 4 6 5 ___8
Comparing 4 and 6 : Since 4 < 6, No Need to exchange
Elements are in phase - 2 : 1 4 6 5 ___8
Comparing 6 and 5 : Since 6 > 5, Exchange (Swap) them
After Pass 2 elements are :
1 4 5 ___6 8
PASS 3 - :
Elements are in phase - 3 : 1 4 5 ___6 8
Comparing 1 and 4 : Since 1 < 4, No Need to exchange
Elements are in phase - 3 : 1 4 5 ___6 8
Comparing 4 and 5 : Since 4 < 5, No Need to exchange
No exchanges in this pass, so the list is sorted
Sorted list is :
1 4 5 6 8
Bubble Sort Program :
BubbleSort.c
#include
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE],i,j,k,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}
printf("Unsorted Elemets :\n");
for(i=0; i<n; i++)
printf("%d ", array[i]);
printf("\n");
/*Bubble sort logic begins here !*/
for(i=0; i<n-1; i++)
{
printf("PASS %d - : \n\n",i+1);
xchanges=0;
for(j=0; j<n-1-i; j++)
{
printf("Elements are in phase - %d : ",i+1);
for(k=0; k<n; k++) { printf("%d ",array[k]); if(k==n-i-1 && k!=n-1) printf("___"); } printf("\n"); printf("Comparing %d and %d : ", array[j], array[j+1]); if( array[j] > array[j+1] )
{
printf("Since %d > %d, Exchange (Swap) them\n",array[j],array[j+1]);
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
xchanges++;
}
else
printf("Since %d < %d, No Need to exchange\n",array[j], array[j+1]);
}
if(xchanges==0) /*If list is sorted*/
{
printf("No exchanges in this pass, so the list is sorted\n\n");
break;
}
printf("After Pass %d elements are : \n",i+1);
for(k=0; k<n; k++)
{
printf("%d ", array[k]);
if(k==n-i-2)
printf("___");
}
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n\n");
}
Output :
Terminal
Enter the number of elements : 6
Enter element 1 : 9
Enter element 2 : 7
Enter element 3 : 5
Enter element 4 : 4
Enter element 5 : 3
Enter element 6 : 2
Unsorted Elements : 9 7 5 4 3 2
PASS 1 - :
Elements are in phase - 1 : 9 7 5 4 3 2
Comparing 9 and 7 : Since 9 > 7, Exchange (Swap) them
Elements are in phase - 1 : 7 9 5 4 3 2
Comparing 9 and 5 : Since 9 > 5, Exchange (Swap) them
Elements are in phase - 1 : 7 5 9 4 3 2
Comparing 9 and 4 : Since 9 > 4, Exchange (Swap) them
Elements are in phase - 1 : 7 5 4 9 3 2
Comparing 9 and 3 : Since 9 > 3, Exchange (Swap) them
Elements are in phase - 1 : 7 5 4 3 9 2
Comparing 9 and 2 : Since 9 > 2, Exchange (Swap) them
After Pass 1 elements are :
7 5 4 3 2 ___9
PASS 2 - :
Elements are in phase - 2 : 7 5 4 3 2 ___9
Comparing 7 and 5 : Since 7 > 5, Exchange (Swap) them
Elements are in phase - 2 : 5 7 4 3 2 ___9
Comparing 7 and 4 : Since 7 > 4, Exchange (Swap) them
Elements are in phase - 2 : 5 4 7 3 2 ___9
Comparing 7 and 3 : Since 7 > 3, Exchange (Swap) them
Elements are in phase - 2 : 5 4 3 7 2 ___9
Comparing 7 and 2 : Since 7 > 2, Exchange (Swap) them
After Pass 2 elements are :
5 4 3 2 ___7 9
PASS 3 - :
Elements are in phase - 3 : 5 4 3 2 ___7 9
Comparing 5 and 4 : Since 5 > 4, Exchange (Swap) them
Elements are in phase - 3 : 4 5 3 2 ___7 9
Comparing 5 and 3 : Since 5 > 3, Exchange (Swap) them
Elements are in phase - 3 : 4 3 5 2 ___7 9
Comparing 5 and 2 : Since 5 > 2, Exchange (Swap) them
After Pass 3 elements are :
4 3 2 ___5 7 9
PASS 4 - :
Elements are in phase - 4 : 4 3 2 ___5 7 9
Comparing 4 and 3 : Since 4 > 3, Exchange (Swap) them
Elements are in phase - 4 : 3 4 2 ___5 7 9
Comparing 4 and 2 : Since 4 > 2, Exchange (Swap) them
After Pass 4 elements are :
3 2 ___4 5 7 9
PASS 5 - :
Elements are in phase - 5 : 3 2 ___4 5 7 9
Comparing 3 and 2 : Since 3 > 2, Exchange (Swap) them
After Pass 5 elements are :
2 ___3 4 5 7 9
Sorted list is :
2 3 4 5 7 9
Happy Learning 🙂