1986 Shell Sort
1986 Shell Sort
1. INTRODUCTION
repeat
k := h[t]; t := t - 1;
for i := k + 1 to N do
begin
u := o[i]; j := i - k;
repeat
if v 2 u[j]thengotoO;
a[j+k]:=a[j]; j:=j-k
until j < 1;
0: o[j + k] := v
end;
until k = 1;
*This research was supported in part by NSF Grant MCS-80-17579 while the author was at
Brown University, and in part while the author was visiting the Institute for Defense Analyses,
Princeton, NJ.
159
0196-6774/86 $3.00
Copyright Q 1986 by Academic Press, Inc.
All rights of reproduciion in my form reserved.
160 ROBERT SEDGEWICK
For example, not even the asymptotic growth of the average case perfor-
mance is known, for the types of sequences used in practice, even though
empirical studies show Shellsort to be among the most efficient sorting
methods available.
In this paper, we consider upper bounds on the worst case running time.
The first results for this problem are due to Papemov and Stasevich [ll].
Their results were extended by Pratt [12], who showed that the worst case
running time is @(N 3/2) for sequences that approximate geometric progres-
sions whose common ratio is an integer, a property which holds for
sequences commonly used in practice. It turns out that this is a significant
restriction, for we are able to exhibit sequences which do not satisfy Pratt’s
property for which the worst case running time is O(N413). The method
used to prove the upper bound is an extension of the previous methods
which leads to a classical problem in number theory, the problem of
Frobenius, and results due to Selmer [13].
This result suggests that even better upper bounds might be possible
(though new results on the Frobenius problem might be needed), and it
suggests sequences of increments that perform better on the average than
those commonly used (though this can only be verified empirically).
Section 2 states Pratt’s results and describes the upper bound proof of
Papemov and Stasevich. Section 3 deals with the Frobenius problem and
Selmer’s results. Section 4 combines these to give the new upper bound
proof. Concluding remarks are offered in Section 5.
2. PREVIOUS BOUNDS
The general argument used for upper bound proofs was given in 1965 by
Papernov and Stasevich [ll]. It involves bounding the time to h/sort in two
ways, then picking the smaller bound for each j and summing on j.
The first bound is simple: when h/sorting, we are dealing with h,
independent files of about N/hi elements each. The worst case running
time for each of these files is O(( N/h,)2) (this tim is required, for example,
when they are in reverse order); therefore hisorting the entire file requires
O(N2/hj) steps in the worst case.
The second bound requires more subtle reasoning. First we need the
fundamental result:
LEMMA 1. If a k-sorted file is h-sorted, it remains k-sorted.
This result goes back at least to Boemer [2]: a proof is given in [8]. 0
Lemma 1 implies that when we come to h/sort a file, it is already
hi+,-sorted and hj+2-sorted. But this means that when we come to any
particular element a[k] during the h/sort, there are many elements which
162 ROBERT SEDGEWICK
are guaranteed to be smaller, and the h/sort will require fewer exchanges.
To bound the number of such elements, we begin with the following
observation:
LEMMA 2. Zf a file is h-sorted and k-sorted, then, for each i,, a[i, - i] I
a[io] whenever i can be expressedas a linear combination with nonnegative
coefficients of h and k.
Proof If i = sh + tk, then afio] 2 a[i, - h] 2 * *. 2 a[i, - sh] since
the file is h-sorted, and a[i, - sh] 2 a[i, - sh - k] 2 * . . 2 a[i, - sh -
tk] = a[i, - i] since the file is k-sorted. •I
The key fact which limits the number of exchanges required by Shellsort
is that if h and k are relatively prime, eventually every integer can be
expressed as a linear combination of h and k. We have
LEMMA 3. Zf h and k are relatively prime, then evev integer greater
than (h - l)( k - 1) - 1 can be representedas a linear combination of h and
k with nonnegative coefficients.
ProoJ: See Knuth [8, Ex. 5.2.1-5.2.21, and discussion in Section 3. q
From these lemmas, we can now prove a second upper bound on the
worst case for hi-sorting. From Lemma 1, when we come to hj-sort a file, it
is already hj +1-sorted and hj+*-sorted. From Lemmas 2 and 3, the elements
which are greater than any particular element i, must be among those
elements within the first (hj+2 - l)(hj+l - 1) positions to the right of i,.
But only one out of each hj of these elements are examined when hj-sort-
ing, so the time to process i, is O(hj+,hj+Jhj). This holds for 1 I i, I N,
so the total time to h,-sort the whole file is 0( Nh,, Ihj+2/hj). This is the
“second bound” that allows the derivation of 0( N 3/2) upper bounds for
many Shellsorts.
If hj+l and h/+2 are O(hj), which holds for the types of increment
sequences which have been used for Shellsort, then we have a simple
tradeoff between the “first bound” of O(N2/hj) and the “second bound”
of 0( Nhj). For example, we have
THEOREM 1 (Papemov-Stasevich). The running time of Shellsort is
0( N3/2) for the increments1,3,7,15,31,63,127,255,. . . ,2j - 1,. . . .
Proof Let hj = 2j - 1. For hj = 0( N ‘12), use the second bound; for
large hj use the first bound. This gives a bound of
where s is determined by
a, = gs + r, O<r<s.
Proofi See Selmer [13] for a proof, a condition for equality, and a
formula for n(a,, u2, Us). 0
SHELLSORT 165
Proof See Johnson [7]. Coupled with Theorem 3, this result can be
developed into a procedure for computing g(u,, u2, u3) whenever it is
defined. [7
These theorems open the possibility that adding a third value can
drastically decrease g. If u2 and us are O(u,) and q, s, and r in Theorem 3
or d in Theorem 4 are O(U:/~), then g(u,, u2, u3) = O(u:i2) not O(u:) as
in the previous bound. In the next section, we exhibit triples that satisfy
these conditions (and the conditions of the theorem) and use them to derive
an improved upper bound for Shellsort.
We are now ready to prove the main result of this paper. The proof
involves a particular sequence of increments that satisfies a host of condi-
tions. After the proof we will discuss how this sequence was discovered and
how others might be found.
N c (4i+l
1 lj<2t/3
+ 3 . 2j + 1)112
N2
+ c = O(N4’3).
2t/32j< t
4j+l + 3 . 2j + 1
166 ROBERT SEDGEWICK
so
The pairs of numbers on each line of this table have the same greatest
common divisor, so we have proved that gcd( hj+i, hj+*) = 1 (and that
gCd(hj+2, hj+3) = 1) for all j. The proof for (hj+l, hj+3) is a similar table:
u V
4’+4 + 3 21+3 + 1 4J+2 + 3. 2J+’ + 1
4J+* + 3 . 2J+’ + 1 4j+2 - 33. 2’+’ - 14
4j+* - 33 2J+’ - 14 9. 2J+3 + 15
3 .4j+2 -99.2’+‘-42 3. 2j+3 + 5
3 ’ 2J+’ + 5 2J+3 + 3
2j+3 + 3 2~+3 - 1
2j+3 - 1 4
2j+3 - 1 1
In the fourth line of this table, u is replaced by 3u, which simplifies the
calculations substantially but which cannot affect the result. This completes
the proof that (h ,+i, hi+*, hi+,) are pairwise relatively prime.
To prove independence of (hj+l, hj+2, hj+3), assume that hj+3 = q,l~~+~
+ 'lhj+* for cO, ci > 0. (This is the only possibility, since hj+3 is the
largest of the three). Clearly c0 < 16 and c, < 4. Substituting and rearrang-
ing terms, we have
All terms on the left in this equation are divisible by 2j+‘, and the right
side cannot be 0, so this implies that 2j+’ divides c0 + ci - 1. But this is
impossible for j > 3, so we must have independence for all
(hj+l, hj+2,
hJ+3
) with j > 3. (In fact, we do not have independence for
j = 1, since 77 = 3 . 23 + 8.)
Independence for j > 3 is sufficient to prove the asymptotic result, since
the contribution of h,, h,, and h, to the total running time is O(N). This
follows from the fact that they are relatively prime in pairs, so the “second
bound” of 0( Nhj) used in Theorem 1 applies. 0
a, = ~4~ + ~2~ + y,
a2 = ~4~+l + ~2~+l + y,
a3 = W4k+2 + X2k+-2 + y,
s = s,2k = so,
t = t,2k = t,,
such that
45yw - 6x2
so = 9yw - 2x2 *
There are similar formulas for si, to, and t,. Integer values for w, x, and y
need to be chosen to make these integers, with the additional constraints
that w, sl, and t, must be positive. For example, the choice w = 1,
x= -3, and y= 1 meets this requirement, as does the choice which is
used in Theorem 5, w = 4, x = 3, and y = 1. Finally, the sequences must
be checked for independence and relative primeness of consecutive triples.
For example, the sequence which derives from the first choice above,
4k - 3 . 2k + 1, fails to satisfy the condition that all consecutive triples
must be pairwise relatively prime. (There are occasional pairs divisible by
17.)
The same asymptotic result is also available for sequences at the other
end of the spectrum, where successive pairs have very large common
divisors.
gCd(hj+l,hj+,) = 2j+2 - 3
The second equation follows from the fact that g(a,, u2, us) < g(a,, u2) <
u1u2 if a,, a, -C us. The rest of the proof proceeds exactly as for Theorem 5.
q
5. CONCLUSION
related for the increment sequence suggested by Knuth [S], the sequence in
Theorem 5, and a more conventional sequence with values close to the
sequence of Theorem 5. For each increment hi, three bounds are given on
the number of exchanges required for h/sorting: the bound from Theorem
1, g(h,+,, h,+,)/h,, which is labeled &; the bound from Theorem 5,
gChj+l* hj+2y hji3)/hj, which is labeled P,; and g(h,+l ,..., h,+,)/h,,
which is labeled at,.
i(3’ - 1)
1 4 13 40 121 364 1093 3280 9841
i-2, 35 117 360 1089 3276 9837 29529 88569 265716
Al, 35 88 277 817 2464 7378 22147 66427 199294
i-2, 35 88 249 735 2193 6567 19689 59055 177153
4’+’ + 1
1 5 17 65 257 1025 4097 16385
0, 63 205 964 4033 16320 65472 262080 1048512
8, 63 164 780 3224 13066 52378 209675 838810
0, 63 164 734 3037 12252 49116 - -
4’+‘+3.2.‘+1
1 8 23 77 281 1073 4193 16577
0, 153 209 925 3898 15992 64759 260599 1045494
0, 153 132 359 853 1867 3909 8003 16193
h2, 153 132 359 798 1540 3044 5865 -
The numbers in the corresponding table for the sequence from Theorem 6
are undefined for the S& row and about a factor of two larger than the
corresponding numbers from the table for the sequence from Theorem 5 for
the other rows. These tables clearly indicate the dramatic reductions in the
upper bound achieved by Theorem 5, and show that some further reduction
is possible by considering more increments.
A second major weakness of the bounds of Theorems 1 and 4 is that they
ignore the fact that substantially less time is required to h/sort when the
file is already sorted in multiples of hi. For example, a file which is
24-sorted and 36-sorted can be 1Zsorted in linear time (using at most N
exchanges). (Note carefully, however,. that the time required to, say, 11-sort
a file which is 12-, 24-, and 36-sorted could be quadratic in the worst case.)
Pratt devised a sequence which exploits this property: in his sequence, twice
and thrice each increment is also in the sequence. This guarantees that the
running time is proportional to N times the number of increments, which is
O((log N)‘). It may be possible to devise a sequence for which some
elements have a low bound because of this divisibility property and others
have a low bound because of “nondivisibility” as suggested by Theorem 5.
Also, it is an interesting problem to extend the “best bound” calculation to
incorporate this divisibility property.
Simulation results show that the type of sequence suggested by Theorem
5 is of practical interest. The table below shows the number of exchanges
SHELLSORT 171
1,5.19,41.109,209,505,929,2161,3905,8749,16001,36449,64769,. ,
literature of the Frobenius problem may help to shed further light on this
fundamental problem.
Nofe udded in proof. A number of related results have been developed in the interval
between original submission and final publication of this paper.
Of most significance is the paper by Ajtai, Komlos, and Szemeredi [l] which settles the
question referred to in the last paragraph: they exhibit a sorting network with just 0( N log N)
comparators. Other networks for various models are given by Leighton [9]. These results make
the search for a Shellsort-based network even more appealing, because such a network would
be far simpler (and likely to be of direct practical utility) and the existence proofs of [l] and [9]
lend credence to the conjecture that an 0( N log N) Shellsort might exist.
Incerpi and Sedgewich [6] have extended the results of this paper to show that the running
time of Shellsort can be made to be O(N’+’ ) for any E > 0, still using only @log N)
increments. Also, they show that this result is the best possible if only a constant number of
previous increments is taken into account in the proof, so that improved results on the
Frobenius problem in the form of extensions to Theorem 3 for four or any constant number of
increments (which seems very difficult) still could not lead to better asymptotic results for
Shellsort. The paper shows further improvements to 0( N’ +“/J’). The increment sequences
used for these results involve large common divisors in successiveincrements, as in Theorem 6.
However, it still seems as though the best increment sequences will involve a combination of
“Frobenius effects” (as in Theorem 5) and “large common divisor effects” (as in Theorem 6),
so new results on the Frobenius problem will be of interest, especially in developing increment
sequences for practical use.
ACKNOWLEDGMENT
REFERENCES
1. AJTAI, KOMLOS, AND SWEREDI, “An 0( n log n) Sorting Network,” Proceedings 15th
Annual ACM Symposium of Theory of Computing, Boston, Mass., April 1983.
2. H. BOERNER,“Darstellung von Gruppen.” Springer-Verlag, Berlin, 1955.
3. A. BRAUER, On a problem of partitions, Amer. J. Muth. 64 (1942). 299-312.
4. H. GREENBERG, An algorithm for a linear diophantine equation and a problem of
Frobenius, Numer. Muth. 34 (1980). 349-352.
5. G. R. [Link] einem Problem von Frobenius, Norske Vid. Selsk. Skr. 5 (1966).
l-37.
6. J. INCERPI AND R. SEDGEWICK,Improved upper bounds on Shellsort, J. Comput. System
Sci. 31(1985), 210-224.
7. S. M. JOHNSON,A linear diophantine problem, Cunad. J. Muth. 12 (1960), 390-398.
8. D. E. KNUTH, “The Art of Computer Programming. 3. Sorting and Searching,”
Addison-Wesley, Reading, Mass., 1973.
9. T. LEIGHTON, “Tight Bounds on the Complexity of Parallel Sorting,” Proceedings 16th
Annual ACM Symposium of Theory of Computing, Washington D.C., April 1984.
10. M. LEWIN, On a linear diophantine problem, Bull. London Murh. Sot. 5 (1973). 75-78.
11. A. A. PAPERNOV AND G. V. STASEVICH,A method of information sorting in computer
memories, Problems inform. Trunsmission l(3) (1965). 63-75.
SHELLSORT 173
12. V. PRATT, “Shellsort and Sorting Networks,” Garland, New York, 1979; Originally
presented as the author’s Ph.D. thesis, Stanford University, 1971.
13. E. S. SELMER, On the linear diophantine problem of Frobenius, J. Reine Angew. Murh.
294 (1977). 1-17.
14. W. J. CURRAN SHARP, “Solution to Problem 7382 (Mathematics),” Educatiod Times,
London, 1884.
15. D. L. SHELL. A high-speed sorting procedure, Comm. Assoc. Comput. Much. 37) (1959).
30-32.