Wednesday, October 26, 2016

Insertion Sort

Tags

Following Program Sort The Array With Insertion Sort

Insertion Sort: In this the elements are inserted in same array by comparing it with previously inserted elements.

#include<iostream>
using namespace std;
main()
{
int a[100],n,i;
cout<<"Enter The No Of Elements in Array ";
cin>>n;
cout<<"Enter The Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
int k;
for(k=1;k<n;k++)
{ int j=k-1;    //for comparing all elements with present element
int temp=a[k];
while(j>0 && temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j]=temp;
}
cout<<"Sorted List Is ";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Special Thank To Ms.Naina For This Program

Output: