C++ program to implement insertion sort


// insertion sort c++

#include<iostream.h>
#include<conio.h>
void main()
{
          int a[20],i,j,n,k,*p,temp;
          p=a;
          clrscr();
          cout<<"Enter the value for n:";
          cin>>n;
          cout<<"\nEnter the elements:";
          for(i=0;i<n;i++)
          {
                   cin>>*(p+i);
          }
          for(k=1;k<=n-1;k++)
          {
                   temp=p[k];
                   j=k-1;
                   while((temp<*(p+j)) && (j>=0))
                   {
                             p[j+1]=p[j];
                             j=j-1;
                   }                                                                                                                                                                                                                                                                                                        p[j+1]=temp;
          }
          cout<<"\nArray after sorting\n\n";
          for(i=0;i<n;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}

No comments: