Monday, 31 October 2011

Program to sort an array using bubble sort


#include<stdio.h>
main()
{
        int n,i,j,temp;
        printf("Enter the number of elements: ");
        scanf("%d",&n);
        int a[n];
        printf("Enter the elements in the array\n");
        for(i=0;i<n;i++)
                scanf("%d",&a[i]);
        for(i=0;i<n;i++)
        {
                for(j=i+1;j<n;j++)
                {
                        if(a[i]>a[j])
                        {
                                temp=a[i];
                                a[i]=a[j];
                                a[j]=temp;
                        }
                }
        }
        printf("\nThe sorted array is - \n" );
        for(i=0;i<n;i++)
                printf("%d, ",a[i]);
}

No comments:

Post a Comment