Tuesday, 8 April 2014

/*write a program to implement bubble sort*/
#include<stdio.h>
    /*function for bubble sort*/
   void bubble_sort(int a[],int n)
  {
          int i,j,t;
          printf("****BUBBLE SORT****\n\n");
               printf("\n\n\tThe unsorted array is: ");
               for(i=0;i<n;i++)
              {
               printf("%d ,",a[i]);
               }

         for(i=n-2;i>=0;i--)
         {
            for(j=0;j<=i;j++)

                  {
                    if(a[j]>a[j+1])
                                    {
                                      t=a[j];
                                     a[j]=a[j+1];
                                     a[j+1]=t;
                                    }
                   }


           }
         printf("\n\n\tSorted array is:\n");
              for(i=0;i<n;i++)
              {
              printf("%d ,",a[i]);
              }

  }

int main()
{
       int t[50],k,limit,i;
       printf("how many element you want to enter: ");
       scanf("%d",&limit);
       printf("enter the array: ");
       for(k=0;k<limit;k++)
       scanf("%d",&t[k]);
       bubble_sort(t,k);

       }
       return 0;

}

No comments:

Post a Comment