F CPP Program To Implement Bubble Sort | CodeTheta

CPP Program To Implement Bubble Sort

December 20, 2015

/* http://native-code.blogspot.com */
#include<iostream.h>
#include<conio.h>
#define MAX 20
class arr
{
  int a[MAX];
  int n;
  public:
  arr(){}
  arr(int*, int);
  void BubbleSort();
  void display();
};

void arr::BubbleSort()
{
  int i, j, t;
  i=j=t=0;
  for(i=0; i<this->n; i++)
  {
    for(j=i+1; j<this->n; j++)
    {
      if(a[i]>a[j])
      {
    t=a[i];
    a[i]=a[j];
    a[j]=t;
      }
    }
  }
}
void arr::display()
{
  int i;
  for(i=0; i<this->n; i++)
  {
  cout<<"/* http://native-code.blogspot.com */";
    cout<<" "<<this->a[i];
  }
}
arr::arr(int a[], int n)
{
  int i;
  this->n=n;
  for(i=0; i<n; i++)
  {
    this->a[i]=a[i];
  }
}
 int main()
{
  clrscr();
  int a[MAX];
  int n, i;
  arr ob;
  n=i=0;
  cout<<"\n/* http://native-code.blogspot.com */";
  cout<<"\nEnter limit: ";
  cin>>n;
  cout<<"/* http://native-code.blogspot.com */";
  cout<<"\nEnter elements:\n";
  for(i=0; i<n; i++)
  {
    cin>>a[i];
  }
  ob=arr(a, n);
  cout<<"/* http://native-code.blogspot.com */";
  cout<<"\nbefore sorting: ";
  ob.display();
  ob.BubbleSort();
  cout<<"\n";
  cout<<"/* http://native-code.blogspot.com */";
  cout<<"\nafter sorting: ";
  ob.display();
  getch();
  return(0);
}
/* http://native-code.blogspot.com */

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.

Post a Comment