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();
}

C++ program to implement selection sort


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