Home » » C Program For Bubble Sort ,Bubble Sorting Using C

C Program For Bubble Sort ,Bubble Sorting Using C

C Program for Bubble Sort ,Bubble Sorting Using C Language

void bubble(int a[],int n)
  {
        int i,j,t;
         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;
                                    }
                   }
      

           }//end for 1.

  }//end function.


  void main()
  {

      int a[100],n,i;

      clrscr();

      printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
      scanf("%d",&n);

      for( i=0;i<=n-1;i++)
            { printf("\n\n Enter integer value for element no.%d : ",i+1);
              scanf("%d",&a[i]);
            }

       bubble(a,n);

       printf("\n\n Finally sorted array is: ");
       for( i=0;i<=n-1;i++)
       printf("%3d",a[i]);

  } //end program.

/*

--------SAMPLE OUTPUT----------------------


Enter integer value for total no.s of elements to be sorted: 6


Enter integer value for element no.1 : 89


Enter integer value for element no.2 : -4


Enter integer value for element no.3 : -67


Enter integer value for element no.4 : 5


Enter integer value for element no.5 : 78


Enter integer value for element no.6 : 11


Finally sorted array is: -67 -4 5 11 78 89

------------------------------------------

*/



0 comments:

Post a Comment