Wednesday, 16 August 2017

12-C-Prog-17.Write a Program to implement Insertion Sort using a 1-D array.


17.Write a Program to implement Insertion Sort using a 1-D array.

 #include<iostream.h>
 void InsSort(int[ ], int);

int main()
 {
   int AR[50],item, n, index;
   cout<<"How many elements do u want to create array with?(max..50)..";
   cin>>n;
   cout<<"\n Enter Array elements ..\n";
   for(int i=0;i<n;i++)
    cin>>a[i];
 
   InsSort(AR, n);
   cout<<"\n \n The sorted array is as shown below \n";
   for(i=0;i<n;i++)
    cout<<AR[i]<<" ";
   cout<<endl;
   return 0;
 }

 void InsSort(int AR[ ], int size)
 {
   int t,j;
   AR[0]=INT_MIN;
   for(int i=1;i<=size;i++)
   {
     t=AR[i];
     j=i-1;
    while(t<AR[j])
    {
      AR[j+1]=t;
      j--;
     }
    AR[j+1]=t;
    cout<<"Array after pass-"<<i<<"-is";
      for(int k =1;k<=size;k++)
       cout<<AR[k]<<" ";
      cout<<endl;
   }
 }
       
 
   

No comments:

Post a Comment