/**** C Program For Implementation Of Bubble Sort. Sorting names entered by the user  *****/
#include< stdio.h>
#include< conio.h>
#define MAX 10
char name[MAX][15];
void sort(int n)
{
 int pa,cp,i,j,k,kk=0;
 char temp[15];
 pa=n-1;
 cp=n-1;
 for(i=1;i< =pa;i++)
 {
  for(j=1;j< =cp;j++)
  {
   kk=kk+1;
   if(strcmp(name[j],name[j+1])>0)
   {
    strcpy(temp,name[j]);
    strcpy(name[j],name[j+1]);
    strcpy(name[j+1],temp);
   }
  }
  printf("\n List after %d pass is ",i);
  for(k=1;k< =n;k++)
   printf("\n\t\t %s",name[k]);
  getch();
 }
 clrscr();
 printf("\n\t\t Total Comparisions Done : %d",kk);
}
void main()
{
 int n,i,j;
 clrscr();
 printf("Enter How Many Names : ");
 scanf("%d",&n);
 if(n>MAX)
  printf("\n\t\tArray Size IS Only %d",MAX);
 else
 {
  printf("\n\t\tEnter %d Names :\n",n);
  for(i=1;i< =n;i++)
  {
   printf("\t\t");
   scanf("%s",name[i]);
  }
  sort(n);
  printf("\n\n\t\tSorted List ");
  for(i=1;i< =n;i++)
   printf("\n\t\t%s",name[i]);
 }
 getch();
}
/********************* OUTPUT *******************
Enter How Many Names : 4
  Enter 4 Names :
  Malcolm
  Lionel
  Mayank
  Pinto
 List after 1 pass is
   Lionel
   Malcolm
   Mayank
   Pinto
 List after 2 pass is
   Lionel
   Malcolm
   Mayank
   Pinto
 List after 3 pass is
   Lionel
   Malcolm
   Mayank
   Pinto
*/
Sunday, June 1, 2008
Subscribe to:
Post Comments (Atom)

2 comments:
nice work
thnks 4 the code
Post a Comment