Tuesday, December 21, 2010

C Program will sort the numbers prompt by user using Bubble Sort method.

#include<stdio.h>

int main()
{
int a[100],i,j,n,b;

printf("How many numbers do you want to sort?  ");
scanf("%d",&n);

printf("Please enter the numbers and press the ENTER key.\n");
for(i=0;i<n;i++)
{
   printf("Enter No.[%d]: ",i+1);
   scanf("%d",&a[i]);
}

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
    if(a[j]>a[j+1])
  {
    b=a[j];
  a[j]=a[j+1];
   a[j+1]=b;
    }
}
}

printf("-------------------\n");
printf("The sorted list is:");
for(i=0;i<n;i++)
{
 printf("\n%d",a[i]);
}
printf("\n-------------------\n");
}

No comments:

Post a Comment