Monday, June 18, 2012

To sort the elements of an array using Bubble Sort


/* Program to sort the elements of an array using Bubble Sort */

#include<stdio.h>

#include<conio.h>

void main()

{

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

  clrscr();

  printf("How many elements are present in the array:");

  scanf("%d",&n);

  printf("\nEnter the values:\n");

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

  {

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

  }

  /* Bubble Sort */

  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("\n\nSorted numbers are:");

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

  {

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

  }

  getch();

}

 

OUTPUT

How many elements are present in the array:10

 

Enter the values:

12

24

68

44

86

72

30

24

18

60

Sorted numbers are:     12      18      24      24      30      44      60     68      72      86

No comments:

Post a Comment

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