Insertion sort

Soluton: 

#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,j,t,n,temp;
cout<<"Enter the array size : "<<endl;
cin>>n;
int a[n];
cout<<"The unsorted array elements are: "<<endl;
for(i=0; i<n; i++) cin>>a[i];
for(i=1; i<n; i++)
{
for(j=i; j>0; j--)
{
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
else break;
}
}
cout<<"The sorted array is :"<<endl;
for(i=0; i<n; i++)
{
cout<<a[i]<<endl;
}
return 0;
}

Leave a comment