Shellsort
Shellsort
If we modify insertion sort (Algorithm 2.2) to h-sort the array and add an outer loop to decrease
h through a sequence of increments starting at an increment as large as a constant fraction of the ar-
ray length and ending at 1, we are led to this compact shellsort implementation.
input S H E L L S O R T E X A M P L E
13-sort P H E L L S O R T E X A M S L E
4-sort L E E A M H L E P S O L T S X R
1-sort A E E E H L L L M O P R S S T X
How do we decide what increment sequence to use? In general, this question is a dif-
ficult one to answer. The performance of the algorithm depends not just on the num-
ber of increments, but also on arithmetical interactions among the increments such as
the size of their common divi-
input S H E L L S O R T E X A M P L E
sors and other properties. Many
13-sort P H E L L S O R T E X A M S L E
different increment sequences
P H E L L S O R T E X A M S L E
P H E L L S O R T E X A M S L E
have been studied in the lit-
4-sort
erature, but no provably best
L H E L P S O R T E X A M S L E
L H E L P S O R T E X A M S L E
sequence has been found. The
L H E L P S O R T E X A M S L E increment sequence that is used
L H E L P S O R T E X A M S L E in Algorithm 2.3 is easy to
L H E L P S O R T E X A M S L E compute and use, and performs
L E E L P H O R T S X A M S L E nearly as well as more sophisti-
L E E L P H O R T S X A M S L E cated increment sequences that
L E E A P H O L T S X R M S L E
have been discovered that have
L E E A M H O L P S X R T S L E
provably better worst-case per-
L E E A M H O L P S X R T S L E
L E E A M H L L P S O R T S X E
formance. Increment sequences
L E E A M H L E P S O L T S X R that are substantially better still
1-sort E L E A M H L E P S O L T S X R
may be waiting to be discovered.
E E L A M H L E P S O L T S X R Shellsort is useful even for
A E E L M H L E P S O L T S X R large arrays, particularly by
A E E L M H L E P S O L T S X R contrast with selection sort and
A E E H L M L E P S O L T S X R insertion sort. It also performs
A E E H L L M E P S O L T S X R well on arrays that are in arbi-
A E E E H L L M P S O L T S X R
trary order (not necessarily ran-
A E E E H L L M P S O L T S X R
dom). Indeed, constructing an
A E E E H L L M P S O L T S X R
A E E E H L L M O P S L T S X R
array for which shellsort runs
A E E E H L L L M O P S T S X R slowly for a particular incre-
A E E E H L L L M O P S T S X R ment sequence is usually a chal-
A E E E H L L L M O P S S T X R lenging exercise.
A E E E H L L L M O P S S T X R As you can learn with
A E E E H L L L M O P R S S T X SortCompare, shellsort is much
result A E E E H L L L M O P R S S T X faster than insertion sort and
Detailed trace of shellsort (insertions) selection sort, and its speed ad-
vantage increases with the array
size. Before reading further, try using SortCompare to compare shellsort with insertion
sort and selection sort for array sizes that are increasing powers of 2 on your computer
(see Exercise 2.1.27). You will see that shellsort makes it possible to address sorting
2.1 ■ Elementary Sorts 261
input
40-sorted
13-sorted
4-sorted
result
problems that could not be addressed with the more elementary algorithms. This ex-
ample is our first practical illustration of an important principle that pervades this
book: achieving speedups that enable the solution of problems that could not otherwise be
solved is one of the prime reasons to study algorithm performance and design.
The study of the performance characteristics of shellsort requires mathematical ar-
guments that are beyond the scope of this book. If you want to be convinced, start
by thinking about how you would prove the following fact: when an h-sorted array is
k-sorted, it remains h-sorted. As for the performance of Algorithm 2.3, the most im-
portant result in the present context is the knowledge that the running time of shellsort
is not necessarily quadratic—for example, it is known that the worst-case number of
compares for Algorithm 2.3 is proportional to N 3/2. That such a simple modification
262 CHAPTER 2 ■ Sorting
Q&A
Q. Sorting seems like a toy problem. Aren’t many of the other things that we do with
computers much more interesting?
A. Perhaps, but many of those interesting things are made possible by fast sorting al-
gorithms. You will find many examples in Section 2.5 and throughout the rest of the
book. Sorting is worth studying now because the problem is easy to understand, and
you can appreciate the ingenuity behind the faster algorithms.
Q. Why so many sorting algorithms?
A. One reason is that the performance of many algorithms depends on the input val-
ues, so different algorithms might be appropriate for different applications having dif-
ferent kinds of input. For example, insertion sort is the method of choice for partially
sorted or tiny arrays. Other constraints, such as space and treatment of equal keys, also
come into play. We will revisit this question in Section 2.5.
Q. Why bother using the tiny helper methods less() and exch()?
A. They are basic abstract operations needed by any sort algorithm, and the code is
easier to understand in terms of these abstractions. Moreover, they make the code di-
rectly portable to other settings. For example, much of the code in Algorithms 2.1
and 2.2 is legal code in several other programming languages. Even in Java, we can use
this code as the basis for sorting primitive types (which are not Comparable): simply
implement less() with the code v < w.
Q. When I run SortCompare, I get different values each time that I run it (and those
are different from the values in the book). Why?
A. For starters, you have a different computer from the one we used, not to mention
a different operating system, Java runtime, and so forth. All of these differences might
lead to slight differences in the machine code for the algorithms. Differences each time
that you run it on your computer might be due to other applications that you are run-
ning or various other conditions. Running a very large number of trials should dampen
the effect. The lesson is that small differences in algorithm performance are difficult to
notice nowadays. That is a primary reason that we focus on large ones!