Insertion Sort

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