#include<iostream.h>
#include<conio.h>
class insertion
{
private:
int a[20], n, i, j, temp, k;
public:
void get();
void sort();
void display();
};
void insertion :: get()
{
cout<<endl<<"Enter the array size : ";
cin>>n;
cout<<endl<<"Enter the array elements"<<endl<<endl;
for(i=0; i<n; i++)
cin>>a[i];
}
void insertion :: sort()
{
for(j=1; j<n; j++)
{
temp=a[j];
i=j;
while((i>=1)&&(temp<a[i-1]))
{
a[i]=a[i-1];
i--;
}
a[i]=temp;
if(j<n)
{
cout<<endl<<"PASS "<< j <<" : ";
for(int k=0; k<n; k++)
cout<<a[k]<<'\t';
}
cout<<endl;
}
}
void insertion :: display()
{
cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<'\t';
}
void main()
{
insertion i;
clrscr();
i.get();
i.sort();
i.display();
getch();
}
Insertion Sort
Share This!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment