Shellsort is a simple extension of insertion sort that allows exchanges of elements that are far apart.
Idea is to rearrange the file to give it the property that takes every $h^{th}$ element yields a sorted file. File is called as a h-sorted file.
h-sorted file is h independent sorted files, interleaved together.
One way to implement shellsort would be, for each h, to use insertion sort independently on each of the h sub files.
Now Big question is how to decide increment value $h$.
There no definitive answer to that but no provably best sequence has been found. In practice we generally we use sequences that decrease geometrically so number of increments is logarithmic in the size of file.
The increment sequence 1,4,13,40,121,364,1093,3280,etc this rule of one-thirds was given by Knuth.
template <class Item>
void shellsort(Item a[],int l , int r){
int h;
for(h = 1; h <= (r-1)/9 ; h = 3*h +1); // this loop to reach end of h max in multiples of 3
for(; h > 0 ; h /= 3)
for(int i = l+h ; i<=r ; i++){
int j = i; Item v = a[i];
while(j>= l+h && v < a[j-h]){
a[j] = a[j-h]; j-=h;
}
a[j] = v;
}
}
Some properties
Shell sort is not stable.