Bubble sort ascending order
#include"stdio.h"
int main()
{
int a[100],n,l,k,swap;
printf("Enter the of Elements:");
scanf("%d",&n);
for(k=0;k<n;k++)
{
printf("Enter a[%d] :",k);
scanf("%d",&a[k]);
}
printf("Elements are :\n ");
for(k=0;k<n;k++)
{
printf("%d\t",a[k]);
}
// sorting //
for(k=0;k<n;k++)
{
for(l=0;l<n;l++)
{
if(a[l]>a[l+1])
{
swap = a[l+1];
a[l+1]= a[l];
a[l]=swap;
}
}
}
printf("\nAscending order are:\n");
for(k=1;k<n+1;k++)
{
printf("%d \t",a[k]);
}
}
Output are:
Enter the of Elements:5
Enter a[0] :12
Enter a[1] :48
Enter a[2] :75
Enter a[3] :16
Enter a[4] :30
Elements are :
12 48 75 16 30
Ascending order are:
12 16 30 48 75
Comments
Post a Comment