C++ program to implement Insertion Sort

#include<iostream>
using namespace std;
void Insertionsort(int *,int);

 

int main()

{

     int n;

     int a[20];

     cout<<endl<<"Enter how many elements :";

     cin >> n;

     for(int i=0;i<n;i++)

     cin>>a[i];

     Insertionsort(a,n);

     for(i=0;i<n;i++)

     cout<<endl<<a[i];

     return 0;

};

 

 

void Insertionsort(int *arr,int n)

{

     int i, j, key;

     for(j = 1; j < n; j++)

    {

                key = arr[j];

                for(i = j - 1; (i >= 0) && (arr[i] > key); i--)

               {

                           arr[i+1] = arr[i];

               }

              arr[i+1] = key;

     }

     return;

}

Input

Enter how many elements : 7

99  12  18  77  1  2  65

Output

1  2  12  18  65  77  99

Leave a Reply

Your email address will not be published.