Friday, 7 December 2012

Bubble sort using CPP

Program to implement bubble sort using c++


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int MAX=50;
class array
{private:
          int arr[MAX];
          int count;
public:
          array()
          {count=0;}
          void add(int);
          void display(void);
          void sort();
};
void array::add(int item)
{        if(count<MAX)
          { arr[count]=item;
             count++;
          }
          else
             cout<<"\n Array is full";
}
void array::display()
{        cout<<"There are "<<count<<" elements in the list\n";
          cout<<"They are : \n\n";
          for(int I=0;I<count;I++)
                   cout<<arr[I]<<"  ";
}
void array::sort()
{        for(int I=0;I<count-1;I++)
          { int flag=1;
                   for(int j=0;j<(count-I-1);j++)
                             if(arr[j]>arr[j+1])
                             {        int t=arr[j];
                                      arr[j]=arr[j+1];
                                      arr[j+1]=t;
                                      flag=0;
                             }
                   if (flag) break;
}
return;
}
void main()
{
          int element,n,I,c;
          array bubble;
          while(1)
          {        clrscr();
                   cout<<"\n Perform the Bubble sort";
                   cout<<"\n-------------------------------------";
                   cout<<"\n\n1.Input data into the list";
                   cout<<"\n2.Display data in the list";
                   cout<<"\n3.Sort the list";
                   cout<<"\n4.Exit from the program";
                   do
                   {        cout<<"\n\nSelect your choice(1:4)";
                             cin>>c;
                             cout<<"";
                   }
                   while((c<1)||(c>4));
                   if(c==4)exit(1);
                   clrscr();
                   switch ( c)
                   {        case 1 : clrscr();
                                         cout<<"How many elements : ";
                                      cin>>n;
                                      for(I=0;I<n;I++)
                                      {        cout<<"Enter a number "<<I+1<< " : ";
                                                cin>>element;
                                                bubble.add(element);
                                      }
                                      break;
                             case 2 : clrscr();
                                      bubble.display();
                                      break;
                             case 3 :
                                      clrscr();
                                      bubble.sort();
                                      cout<<"List is successfully sorted \n";
                                      break;
                   }
                   cout<<"\nPress any key to continue................";
                   getch();
          }
}


No comments:

Post a Comment