Friday 15 April 2016

INSERTION SORT..........

#include<iostream>
using namespace std;
class sorting
{
int a[10],i,n;
public:
sorting()
{
cout<<"Number of Element:-";
cin>>n;
cout<<"Unsorted Element:-";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
}
void insertion()
{
for(int j=2;j<=n;j++)
{
i=j-1;
int key=a[j];
while(i>0&&a[i]>key)
{
a[i+1]=a[i];
i--;
}
a[i+1]=key;
}

}
void disp()
{
cout<<"Sorted Element:-";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
}
};
int main()
{
sorting x;
x.insertion();
x.disp();
}

No comments:

Post a Comment