In: Computer Science
Bubble sort for 12, 2, 3, 21, 11, 10,8
Babul sort:
Babul sort is sorting algorithm which is used to sort the elements
here for every element we compare with the preivious element if the element is smaller then we swap the element.
This process continues for every element untill the array is sorted
Code:
#include<stdio.h>
int main()
{
int x[7]={12,2,3,21,11,10,8},i,j,tem,k;
/*Declaring the variables*/
for(i=0;i<7;i++)
{
printf("%d ",x[i]);
/*Here we print the array
elements*/
}
printf("\n");
for(i=0;i<7;i++)
{
/*This loop iterated for every
elements*/
for(j=0;j<7-i-1;j++)
{
if(x[j]>x[j+1])
{
/*If the current element is greater than the
next element
Then we swap the elements*/
tem=x[j];
x[j]=x[j+1];
x[j+1]=tem;
for(k=0;k<7;k++)
{
printf("%d ",x[k]);
/*Here we print the array
after every swap*/
}
printf("\n");
}
}
}
}
Output:
Here you can see how elements are sorted
Indentation: