Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to implement Bubble sort


#include<iostream.h>
#include<iomanip.h>
class bubblesort
{

private:
     int *x;
     int items;
public:
     bubblesort(int);
     ~bubblesort();
     void input(int []);
     void display();
     void sort();
};


bubblesort::bubblesort(int n)
{
     items=n;
     x=new int[items];
}


bubblesort::~bubblesort()
{
     delete [] x;
}


void bubblesort::input(int a[])
{

     for(int i=0;i<items;i++)
     x[i]=a[i];
}


void bubblesort::sort()
{

     for(int i=0;i<items;i++)
     {
            for(int j=0;j<items-(i+1);j++)
            {
               if(x[j]>x[j+1])
               {
                    int temp;
                    temp=x[j];
                    x[j]=x[j+1];
                    x[j+1]=temp;
               }
          }
     }
}



void bubblesort::display()
{
     for(int i=0;i<items;i++)
     cout<<" "<<x[i];
}



int main()
{
     int a[100],n;
     cout<<"\n Enter how many elements";
     cin>>n;
     cout<<"\n Enter "<<n<<"elements";
     for(int i=0;i<n;i++)
     cin>>a[i];
     bubblesort obj(n);
     obj.input(a);
     obj.sort();
     obj.display();
     return 0;
}


Test data

Enter how many elements
6
Enter 6 elements
34  18  22  11  88  13

Output
11  13  18  22  34  88