#include<iostream.h>
#include<conio.h>
class shell
{
private:
int a[10], n, i, k, temp, dis, swap;
public:
void get();
void sort();
void disp();
};
void shell :: 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 shell :: sort()
{
dis=n/2;
do
{
static int s=0;
s++;
do
{
swap=0;
for(i=0; i<n-dis; i++)
{
if(a[i]>a[i+dis])
{
temp=a[i];
a[i]=a[i+dis];
a[i+dis]=temp;
swap=1;
}
}
}while(swap);
cout<<endl<<"PASS "<< s <<" : ";
for(int k=0; k<n; k++)
cout<<a[k]<<'\t';
cout<<endl;
}while(dis=dis/2);
}
void shell :: disp()
{
cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<'\t';
}
void main()
{
shell s;
clrscr();
s.get();
s.sort();
s.disp();
getch();
}
Shell Sort
Share This!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment