Data Structures and Algorithms - Old Questions

5. Write a program in C for bubble sorting.

5 marks | Asked in 2068

#include<stdio.h>

int main()

{

    int a[10],i,j,temp,n;

    printf("\\n Enter the max no.of Elements to Sort: \\n");

    scanf("%d",&n);

    printf("\\n Enter the Elements : \\n");

    for(i=0; i<n; i++)

    {

        scanf("%d",&a[i]);

    }

    for(i=0; i<n; 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;

            }

        }

    }

    printf("The sorted array is: \\n");

    for(i=0; i<n; i++)

    {

        printf("%d\\t",a[i]);

    }

    return 0;

}