Friday 1 April 2016

INSERTION SORT C++

#include<iostream>
using namespace std;
class xyz
{
    int a[10],n;
    public:
        xyz();
        void sort();
        void disp();
};
xyz::xyz()
{
    cout<<"Enter no of Element:-";
    cin>>n;
    cout<<"Enter Unsorted Number....";
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
}
void xyz::sort()
{
    for(int j=1;j<n;j++)
    {
        int i=j-1;
        int key=a[j];
        while(i>=0&&a[i]>key)
        {
            a[i+1]=a[i];
            i--;
        }
        a[i+1]=key;
    }
}
void xyz::disp()
{
    for(int i=0;i<n;i++)
    {
        cout<<a[i];
    }
}
int main()
{
        xyz x;
        x.sort();
        x.disp();   
}

       
       

No comments:

Post a Comment