Earn without any Investment!

Wednesday, October 20, 2010

Bubble Sort

#include<iostream.h>
#include<conio.h>
class bubble
{
 public:
     int a[10],i,j,k,n,temp,xchange;
     void getdata();
     void disp();
     void sort();
     void putdata();
};
void bubble::getdata()
{
 cout<<"\n Enter the no. of element:";
 cin>>n;
 cout<<"Enter the element one by one:";
 for(i=0;i<n;i++)
 {
  cin>>a[i];
 }
}
void bubble::putdata()
{
 cout<<"\n Unsorted list:";
 for(i=0;i<n;i++)
 {
  cout<<"\n"<<a[i];
 }
}
void bubble::sort()
{
 for(i=0;i<n;i++)
 {
  xchange=0;
  for(j=0;j<n-1;j++)
  {
   if(a[j]>a[j+1])
   {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    xchange++;
   }
 }
 if(xchange==0)
 break;
 cout<<"\n After passsing"<<i+1<<"Elements are:";
 for(k=0;k<n;k++)
 {
  cout<<"\n"<<a[k];
 }
 }
 }
 void bubble::disp()
 {
  cout<<"\n Sorted list is:";
  for(i=0;i<n;i++)
  {
   cout<<"\n"<<a[i];
  }
 }
 void main()
 {
 clrscr();
 bubble b;
 b.getdata();
 b.putdata();
 b.sort();
 b.disp();
 getch();
 }

No comments:

Post a Comment