Search This Blog

Thursday, October 13, 2011

Write a Program For Sorting An Array by Bubble Sort.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,n;
   6:  float x[50];
   7:  printf("Enter the number of elements in array:");
   8:  scanf("%d",&n);
   9:  for(i=0;i<n;i++)
  10:  {
  11:  printf("\nEnter the element no %d:",i);
  12:  scanf("%f",&x[i]);
  13:  }
  14:  printf("\nOriginal array is:\n");
  15:  for(i=0;i<n;i++)
  16:  printf("%.2f ",x[i]);
  17:  printf("\n");
  18:  for(j=0;j<n-1;j++)
  19:      for(i=0;i<n-1;i++)
  20:      {
  21:      if(
  22:      x[i]>x[i+1])
  23:          {
  24:          float p,q;
  25:          p=x[i];
  26:          q=x[i+1];
  27:          x[i]=q;
  28:          x[i+1]=p;
  29:          }
  30:      }
  31:  printf("\nThe sorted array is:\n");
  32:  for(i=0;i<n;i++)
  33:  printf("%.2f ",x[i]);
  34:  printf("\n");
  35:  getch();
  36:  }

No comments:

Post a Comment