Bubble Sort


Write a program to perform bubble sort.

/* Program to perform bubble sort. */

/* In this bubble sort the element will be sort in ascending order. */

#include<stdio.h>

#include<conio.h>

void bs(int[], int);

main()

{

/*here ne means number of elemnts in array.*/

/*here a means array.*/

/*here bs means bubble sort. */

int a[50],ne,i;

printf ("Enter how many elements you wants to perform bubble sort = ");

scanf("%d",&ne);

printf ("Enter the elements in array :-\n");

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

{

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

}

bs(a,ne);

printf ("Elements after sort :-\n");

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

{

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

}

}

void bs (int a[], int ne)

{

    /*Here temp means temporary for story data for temporarily. */

    int pass, element, temp;

    for (pass=ne-1;pass>=1;pass--)

    {

    for (element=0;element<pass;element++)

    {

        if (a[element] > a[element+1])

        {

                temp=a[element];

        a[element]=a[element+1];

        a[element+1]=temp;

    }

}

    }

}