Bubble Sort

 #include<iostream.h>  
 #include<conio.h>  
 class bubble  
 {  
      private:  
           int a[20], n, i, j, temp;  
      public:  
           void get();  
           void sort();  
           void display();  
 };  
 void bubble :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<"Enter the array elements"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void bubble :: sort()  
 {  
      for(i=0; i<n-1; i++)  
      {  
           for(j=0; j<n-1-i; j++)  
           {  
                if(a[j]>a[j+1])  
                {  
                     temp=a[j];  
                     a[j]=a[j+1];  
                     a[j+1]=temp;  
                }  
           }  
           if(i<n)  
           {  
                cout<<endl<<"PASS "<< i+1 <<" : ";  
                for(int k=0; k<n; k++)  
                     cout<<a[k]<<'\t';  
           }  
           cout<<endl;  
      }  
 }  
 void bubble :: display()  
 {  
      cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<a[i]<<'\t';  
 }  
 void main()  
 {  
      bubble b;  
      clrscr();  
      b.get();  
      b.sort();  
      b.display();  
      getch();  
 }  

Share This!


No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive