Monday 7 October 2013

c program to sort elements of an array using bubble sort

"The idea of bubble sort is to scan through the list and swap the adjacent elements that are placed in wrong order"  










#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,z,j;
clrscr();
printf("enter the size of array");
scanf("%d",&n);
printf("enter the elements of array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=1;j<=n;j++)
{
if(a[j-1]>a[j])
{
 z=a[j-1];
 a[j-1]=a[j];
 a[j]=z;
}
}
}
printf("\n\nafter sorting..");
printf("\nelements are:\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}

No comments:

Post a Comment