Insertion Sort :

<br />#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int a[10],n,j,temp;
cout<<"--------Insertion Sort---------"<<endl<<endl;
cout<<"Enter the size of array:"<<endl;
cin>>n;
cout<<"Enter Elements:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
temp = a[i];
j=i-1;
while((temp < a[j]) && (j>=0))
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
cout<<endl<<"After Sorting Array is:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl<<" @Myprogworld...!!"<<endl;
getch();
return 0;
}

Output :-

insertion sort

Leave a comment