Tuesday, August 25, 2009

bubble sort

#include"stdio.h"
#include"conio.h"

int i,j,n,temp,a[20];
void bubble(int a[],int n);

void main()
{
clrscr();
printf("enter the limits");
scanf("%d",&n);

printf("enter the elements wants to sort\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
printf("\n%d",a[i]);

bubble(a,n);
getch();
}


void bubble(int a[20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j>n-1;j++)
{
if(a[j] >a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

}
}
printf("the sorted list is:\n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}

No comments:

Post a Comment