Insertion Sort
Program In C++:-
//Insertion Sort
//Insertion Sort
#include <iostream>
using namespace std;
void InsertionSort(int a[],int n)
{
int i,j,temp;
for(j=1;j<n;j++)
{
temp=a[j];
i=j;
while(i>0 && a[i-1]>=temp)
{
a[i]=a[i-1];
i--;
}
a[i]=temp;
}
cout<<"Displaying the values:";
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
}
int main()
{
int n;
cout<<"Enter the size of array:";
cin>>n;
int a[n];
cout<<"Enter the values in Array:\n";
for(int i=0;i<n;i++)
cin>>a[i];
InsertionSort(a,n);
}
/*
Output
Enter the size of array:3
Enter the values in Array:
5
1
4
Displaying the values:
1
4
5
*/
Comments