Monday 8 January 2018

Bubble Sort program code using C language

//Program for Bubble Sort
#include<stdio.h>
void main()
{
int a[10],n,i,j,t;
clrscr();
printf("How many numbers:");
scanf("%d",&n);
printf("Enter the numbers :");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
printf("The sorted list ...");
 for(i=0;i<n-1;i++) //for ‘n’ elements n-1 passes
 {
  for(j=0;j<n-1-i;j++) //and each time n-1-I comparisons
  {
   if(a[j]>a[j+1])
    {
    t=a[j];
    a[j]=a[j+1];
    a[j+1]=t;
    }
  }
 }
for(i=0;i<n;i++)
 printf("\n\t\t%d",a[i]);
getch();
}


No comments:

Post a Comment