Saturday, August 20, 2011

Sort an array using Bubble sort.

//Program to sort an array using Bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
  int *a,n,i,t,k;
  clrscr();
  printf("\nEnter the no. of element in the array: ");
  scanf("%d",&n);
  a=(int *)malloc(n* sizeof(int));
  printf("\nEnter the values of %d elements: ",n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(k=0;k<n-1;k++)
  {
    for(i=0;i<n-1-k;i++)
    {
      if(a[i]>a[i+1])
      {
       t=a[i];
       a[i]=a[i+1];
       a[i+1]=t;
      }
    }
  }
 printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
      printf("\n %d",a[i]);
  }
  getch();
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More