C Code 6 _ To Sort the given elements in ascending order using Bubble Sort

C Code 6 _ To Sort the given elements in ascending order using Bubble Sort


Program :


#include<stdio.h>
int main()
{
    int a[50];
    int i,j,n,temp;
    printf(" * * * * * * * * * * * Bubble Sort * * * * * * * * * * * * * *");
    printf("\n\nEnter the number of elements to to sorted : ");
    scanf("%d",&n);
    printf("\n\nEnter %d elements : ",n);
    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    printf("\n\nSorted array is : ");
    for(i=0;i<n-1;i++)
    {
         for(j=0;j<n-i-1;j++)
         {
               if(a[j]>a[j+1])
               {
                     temp=a[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
               }
         }
    }
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }
    getch();
    return 0;
}


Output:






Thank you ...
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts