Shell Sort
Posted by Vinod on September 22, 2006
/*********************************************************
-> This C++ program is to perform Shell sort.
-> This program works in microsoft vc++ 6.0 environment.
-> The numbers are sorted in increasing order.
**********************************************************/
#include<iostream.h>
class sorting
{
private:
double *array;
int n;
public:
void input();
void shellsort();
void output();
void swap(double& g,double& f);
};
void sorting::input()
{
cout<<”****************************************************\n”
<<”This program sorts numbers in increasing order”
<<”\n\t\tusing shell sort technique\n”
<<”****************************************************\n”;
cout<<”Enter how many numbers you are going to enter for sorting ::”;
cin>>n;
array=new double[n+1];
cout<<”Now enter your elements ::\n”;
for(int i=0;i<n;i++)
cin>>array[i];
}
void sorting::swap(double &f,double &g)
{
double temp;
temp=f;
f=g;
g=temp;
}
void sorting::shellsort()
{
int i,j,inc=3;
double temp;
while(inc>0)
{
for(i=0;i<n;i++)
{
j=i;
temp=array[i];
while((j>=inc)&&array[j-inc]>temp)
{
array[j]=array[j-inc];
j=j-inc;
}
array[j]=temp;
}
if(inc/2!=0)
inc=inc/2;
else if(inc==1)
inc=0;
else
inc=1;
}
}
void sorting::output()
{
cout<<”Now the sorted numbers are ::\n”;
for(int i=0;i<n;i++)
cout<<array[i]<<’\t’;
cout<<endl;
}
int main()
{
sorting obj;
obj.input();
obj.shellsort();
obj.output();
return 0;
}
/*********************************************************************
SAMPLE OUTPUT ::
****************************************************
This program sorts numbers in increasing order
using shell sort technique
****************************************************
Enter how many numbers you are going to enter for sorting ::7
Now enter your elements ::
1.7
1.6
1.5
1.4
1.3
1.2
1.1
Now the sorted numbers are ::
1.1 1.2 1.3 1.4 1.5 1.6 1.7
Press any key to continue
***********************************************************************/
index for c++ programs to implement sorting techniques « Data Structures through C & C++ for beginners said
[...] 6) Shell sort [...]