21)Write a C program to sort an array elements using Bubble sort method.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,j,k,tmp;
clrscr();
printf(“\n\nHOW MANY ELEMENTS U WANT”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nENTER THE ELEMENT a[%d]\t”,i);
scanf(“%d”,&a[i]);
}
printf(“\nUNSORTED LIST IS \n\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
printf(“\n\nELEMENTS AFTER PASS:-%d \t\n”,i);
for(k=0;k<n;k++)
{
printf(“%d\t”,a[k]);
}
}
printf(“\nSorted List is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s