Wednesday, 29 October 2014

bubble sort

#include<stdio.h>
int main()
{
    int a[10],i,n,pass,tmp=0;
    printf("\n Enter the size of the array:");
    scanf("%d",&n);
    printf("\n Enter the elements of the array:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    pass=1;
    while(pass<n)
    {
        for(i=0;i<n-pass;i++)
        {
            if(a[i]>a[i+1])
            {
                tmp=a[i];
                a[i]=a[i+1];
                a[i+1]=tmp;
            }
        }
        pass=pass+1;
    }
    printf("\n The sorted array in ascending order is as follows:\n");
    for(i=0;i<n;i++)
    {
        printf("%d \n",a[i]);
    }
    return 0;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.