#include<iostream.h>
#include<conio.h>
class insertion
{
private:
int a[10],n;
public:
void read();
void sort();
void show();
};
void insertion::read()
{
cout<<"Enter how many elements you want to sort : ";
cin>>n;
cout<<"Enter "<<n<<" elements to sort : ";
for(int i=0;i<n;i++)
cin>>a[i];
}
void insertion::sort()
{
int k;
for(int j=1;j<n;j++)
{
k=a[j];
for(int i=j-1;i>=0&&k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
}
}
void insertion::show()
{
cout<<"Sorted elements are : ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
void main()
{
clrscr();
insertion i;
i.read();
i.sort();
i.show();
getch();
}
No comments:
Post a Comment