HERE IS SIMPLEST WAY TO PERFORM INSERTION SORT
JUST COPY THE BELOW GIVEN CODE IN YOUR TC FOLDER AND ENJOY IT....
#include<iostream.h>
#include<conio.h>
class insertion_sort
{
int a[50];
int i,j,k,n,temp,l,m,pos;
public:
void getarray();
void display();
};
void insertion_sort::getarray()
{
cout<<"\nEnter the size of the array(max 50)"; cin>>n;
cout<<"\n Enter the array"; for(l=1;l<=n;l++) cin>>a[l];
}
void insertion_sort::display()
{
for(i=2;i<=n;i++)
{
temp=a[i];pos=i-1;
while(temp<a[pos])
{
a[pos+1]=a[pos];
pos--;
}
a[pos+1]=temp;
}
for(m=1;m<=n;m++)
cout<<a[m]<<"\t";
}
void main()
{
clrscr();
insertion_sort obj;
cout<<"\t ///////////////////INSERTION SORT///////////////////";
obj.getarray();
cout<<" sorted array"<<"\n";
obj.display();
getch();
}
We continue looking at sorting algorithms, this time we take a look at Insertion Sort. Insertion is better than bubble sort, but still very poorly behaving.
Really proud of you...
ReplyDelete