Bubble sort in c programming

Bubble Sort : It is a sorting algorithm which repeatedly swaps adjacent elements if they are in wrong order arranges them in ascending order.

#include<stdio.h>
int main()
{
    int a[50],n,i,j,temp;
    printf("Enter the size of array: ");
    scanf("%d",&n);
    printf("Enter the array elements: ");
    
    for(i=0;i<n;++i)
        scanf("%d",&a[i]);
        
    for(i=1;i<n-2;++i)
        for(j=0;j<n-2;++j)
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
            
    printf("\nArray after sorting: ");
    for(i=0;i<n;++i)
        printf("%d ",a[i]);

    return 0;

}


Comments

Popular posts from this blog

Finding the second largest in an array