These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
##Shellsort
####Concept
- sequence of H-Sorts (with differing
h
values) which result in a fully sorted array. - sequence of
h
values must be well chosen as it has a significant effect on the runtime and efficiency of the operation
#####Choosing h
Formulaic
It is possible to choose a sequence according to a formula.
An example of a simple bad choice is sequence that follows the form 2^x
(powers of two):
2 4 8 16 ...
There are only even values of h
meaning that even values are never compared against odd values.
A better formula, 3x + 1
, was proposed by Knuth and produces better values of h
.
4 7 10 13 ...
Emperical
A series may also be found empirically/heuristically.
Segwick proposed a sequence of h-values was found empirically as:
1, 5, 19, 41, 109, 209, 505, 929...
This sequence is very good in practice.
####Implementation
- loop which changes values of
h
- H-Sort implemented in the loop
So Shellsort is relatively easy to program and has very little code all in all. Because of this, Shellsort is often used in hardware and embedded systems where space is still problematic.
####Analysis
The mathematical analysis of Shellsort is non-trivial, as it differs with different sequences of h
.
Formulaic sequences can sometimes be analyzed e.g. 3x+1
is bounded approximately by O(N^(3/2))
.
Emperical sequences can rarely be characterized mathematically, and thus analyzed.
In all, very few h-value sequences in use have proper analysis today. Analysis of Shellsort is still a topic under being researched.
####Verdict Fast and compact, but hard to fully characterize.