Tuesday, 19 November 2013

Bubblesort in program c program - Bubblesort program in data structures

Aim: To write a Bubble Sort Program using C program

Program :

#include<stdio.h>

void main()

{

       int a[20],n,i;

       clrscr();

       printf(" Enter the size of the list");

       scanf("%d",&n);

       printf(" Enter the elements :");

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

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

       bubblesort(a,n); //sort the elements using bubble sort

       printf(" Bubble sort");

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

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

       getch();

}

bubblesort(int a[],int n)

{

      int i,j,temp;

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

    }

    }

      }

 }

Output:



No comments:

Post a Comment