Sunday, August 21, 2011

How to Sort array in c++ using Bubble sort.

//How to Sort array in c++ using Bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
  int a[40],n,i,t,k;
  clrscr();
  cout<<endl<<"Enter the no. of element in the array: ";
  cin>>n;
  cout<<endl<<"Enter the values of "<<n<<" elements: ";
  for(i=0;i<n;i++)
  {
    cin>>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;
      }
    }
  }
  cout<<endl<<"The sorted array is:";
  for(i=0;i<n;i++)
  {
    cout<<endl<<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