THE WORST CASE IN SHELLSORT AND RELATED ALGORITHMS

BJORN POONEN

Abstract. We show that sorting a sufficiently long list of length N using Shellsort with m increments (not necessarily decreasing) requires at least N^{1+c/\sqrt{m}} comparisons in the worst case, for some constant c > 0. For m ≤ (log N/ log log N)^{2} we obtain an upper bound of the same form. We also prove that Ω(N(log N/ log log N)^{2}) comparisons are needed regardless of the number of increments. Our approach is general enough to apply to other sorting algorithms, including Shaker-sort, for which an even stronger result is proved.

1. Introduction

Shellsort is a general-purpose sorting algorithm that was invented by Shell in 1959 [14]. Empirical results show that it is competitive with the fastest sorting algorithms, especially when N, the number of elements to be sorted, is not too large. The algorithm is very simple to describe. If h ≥ 1, to h-sort a list L[1], …, L[n] is to apply insertion sort to each of the sublists L[i], L[i+h], …, L[i+2h], etc., where 1 ≤ i ≤ h. A list for which these sublists are in order is said to be h-ordered. Shellsort sorts a list by performing an h_{1}-sort, an h_{2}-sort, …, and finally an h_{m}-sort, where h_{1}, h_{2}, …, h_{m} are positive integers called the increments and h_{m} = 1. The choice h_{m} = 1 guarantees that the resulting list is in order, because 1-sorting a list is the same as sorting it using insertion sort.

As far as complexity is concerned, insertion sort is a series of compare-exchange operations between pairs of adjacent elements, where a compare-exchange consists of comparing two elements and exchanging them, if necessary, to put them in order. Thus Shellsort also can be considered a series of compare-exchanges, between pairs of elements L[i] and L[i + h] for various h’s (the increments).

Shaker-sort, proposed by Incerpi and Sedgewick [5], is another general-purpose sorting algorithm which sorts a list in a number of passes, each having an associated increment h. To h-shake a list L[1], …, L[n] is to perform compare-exchanges on the following pairs of positions, in the order listed:

(1, 1 + h), (2, 2 + h), …, (N − h − 1, N − 1), (N − h, N),
(N − h − 1, N − 1), (N − h − 2, N − 2), …, (2, 2 + h), (1, 1 + h).

Note that the distance between the elements of each pair is h. Shaker-sort sorts a list by performing an h_{1}-shake, an h_{2}-shake, …, and an h_{m}-shake, for some sequence h_{1}, h_{2}, …, h_{m}. Instead of assuming h_{m} = 1, which would not guarantee that the result was sorted, we assume that insertion sort is applied at the end, after the h_{m}-shake. (Alternatively, we could perform 1-shakes repeatedly at the end until the list is sorted.)

Date: October 11, 1992.

For the final version of this article, see J. Algorithms 15, no. 1 (July 1993), 101–124.

Often one defines an infinite sequence of integers and takes the integers less than N, in decreasing order, as the increments. Such increment sequences are said to be uniform. But in general, the increment sequence need not be decreasing, and it may depend on N.

Choosing the increment sequence wisely is crucial for good performance, both in theory and in practice, so much research has gone into finding the best sequence. Shell [14] suggested using (⌊N/2⌋, ⌊N/4⌋, ⌊N/8⌋, …, 1) in his algorithm, but when N is a power of 2, this sequence requires Θ(N^{2}). (All bounds listed here are for the worst case.) Lazarus and Frank [7], Hibbard [3], and Knuth (Section 5.2.1 in [6]) proposed modifications to Shell’s sequence, and Papernov and Stasevich [9] showed that Hibbard’s sequence required only O(N^{3/2}) comparisons. All of these sequences were within a constant of a geometric sequence with integer ratio, and Pratt [10] showed that Shellsort with any such sequence would require Ω(N^{3/2}) comparisons. Pratt also gave a sequence with Θ((log N)^{2}) increments that required only Θ(N(log N)^{2}) comparisons. (See Proposition 4 in this paper.) But this sequence gave a slow algorithm in practice, because of the large number of increments involved.

For a while it was believed, because of Pratt’s lower bound, that Shellsort with any sequence of O(log N) increments would require Ω(N^{3/2}) comparisons. But then Sedgewick [11] gave an example of such a sequence which required only O(N^{4/3}) comparisons, and Incerpi and Sedgewick [4] later reduced the upper bound to O(N^{1+ϵ/\sqrt{log N}}), for any ϵ > 0. Weiss and Sedgewick [16] made a conjecture about inversions which implied that this bound was best possible. We prove a version of this conjecture, and hence, using an argument similar to theirs, we can deduce that this is the best possible bound for O(log N) increments.

Very recently, Cypher [2] proved that all Shellsort sorting networks based on monotonically decreasing increment sequences require Ω(N(log N)^{2}/ log log N) comparators. The differences between this lower bound and ours are the following:

• Most importantly, his lower bound applies only to Shellsort networks. This means that the entire sequence of comparisons must be specified before looking at the given list. By contrast, when Shellsort is used as a general-purpose sorting algorithm, the results of previous comparisons are used in choosing the comparisons to be done next. For example, the part of the algorithm that inserts an element into a sorted list moves the element only as far as it needs to be moved. With a network, one would have to allow for the possibility that the element would move the entire length of the list, unless something was already known about the size of the element.

• Our results apply to other algorithms, such as Shaker-sort.

• We do not require that the increments be decreasing.

• We do not require that the h-sort be done by insertion sort. It may be done by any series of compare-exchanges on pairs separated by h.

• We have a bound as a function of the number of increments, in addition to a general bound.

• Cypher’s bound is slightly stronger than ours: Ω(N(log N)^{2}/ log log N) vs. Ω(N(log N/ log log N)^{2}).

We now outline the rest of our paper. First, in Section 2, we discuss the generalizations of Shellsort to be considered. In Section 3, we describe the Frobenius problem, and prove a result showing its connection with Shellsort. This result is used in Section 4 to obtain upper bounds on the worst case of Shellsort, as a function of the number of increments used.

The rest of the paper is devoted to proving the corresponding lower bound theorems. Section 5 defines inversions, which are used to measure how unordered a list is. Sections 6 and 7 discuss the Frobenius problem in greater depth. These two sections contain the hardest part of the argument. The results of those two sections are used in Section 8 to show that that a certain list, the Frobenius pattern, has many inversions even though it is h-ordered for many h’s. Section 9 uses the Frobenius pattern to build other such lists. Because Sections 6, 7, 8, and 9 involve so much detail, it may be best for the reader to skip them on a first reading, and to look at only the important Lemma 18, which is the only result from those sections that is used later.

Finally, in Section 10 we prove three lower bound theorems. Theorem 3 says that if not enough (only m) increments are used, many (N^{1+c_{2}/\sqrt{m}} for some constant c_{2}) compareexchanges will have to be done in the worst case. This is proved by dividing [\sqrt{N}, N/2] into intervals and showing that if some interval contains few increments, then Lemma 18 can be applied to give a list with many inversions so that many compare-exchanges are needed to sort the list. Theorem 4 gives a lower bound on the worst case which is independent of the number of increments. It follows easily from Theorem 3. The argument is as follows: the number of comparisons per increment is always at least Ω(N). So if the number of increments m is large, many comparisons are needed. On the other hand, if m is small,, again many comparisons are needed by Theorem 3. The last result of the section, Theorem 5, is a strengthening of the lower bound for Shaker-sort for certain increment sequences. This was proved by Weiss and Sedgewick [15], assuming their inversion conjecture.

In proving our theorems, we do not attempt to obtain the best possible constants. Also, wherever convenient, we assume N is sufficiently large. (At the very end of the proof of Theorem 3, we need N ≥ 2^{2^{36}}, for example!) We could certainly make our theorems work for smaller N and improve our constants by making more precise estimates in the course of the proof, but it is probably not worth the effort, because it seems nearly impossible to improve the result so much that it gives a meaningful general lower bound for any N < 10^{12}. Nevertheless, we feel that the existence of an Ω(N(log N/ log log N)^{2}) bound suggests that even in practical situations, Shellsort may not be the best algorithm to use when N is large, say N > 10^{7}.

We end our paper by mentioning a few unanswered questions.

2. Shellsort-type Algorithms

We now describe the general class of algorithms we will consider. A sorting algorithm is a Shellsort-type algorithm if it satisfies the following:

(1) The algorithm sorts a list L[1], …, L[N] in a number of passes, each with an associated increment h. During a pass with increment h, the algorithm performs compareexchanges (one at a time) on pairs L[i], L[i + h] for 1 ≤ i ≤ N − h.

(2) At least Ω(N) comparisons are done during each pass with increment h < N/2.

(3) If the list is k-ordered at the beginning of a pass, the list is k-ordered at the end of the pass also.

The sequence of increments need not be decreasing, and it need not end in 1, so long as the algorithm actually sorts. It may contain duplicates. The sequence also may depend on N, but it must be fixed before looking at the list to be sorted.

Because of condition 3, it is not obvious that even Shellsort is a Shellsort-type algorithm. (That Shellsort satisfies 3 is Theorem K in Section 5.2.1 of [6].) Even more surprising is that Shaker-sort satisfies 3. To prove this, we will use the following easy result, which is Exercise 21 in Section 5.3.4 of [6].

Proposition 1. If L, L′ are lists of the same length N, and L[i] ≤ L′[i] for all i, then the inequalities still hold after the same series of compare-exchanges is performed on both lists.

Proof. It suffices to consider a single compare-exchange, say on (j, k), with j < k. Let M and M′ be the lists after the compare-exchange. For i ≠ j, k, M[i] = L[i] ≤ L′[i] = M′[i]. Also

M[j] = min{L[j], L[k]} ≤ min{L′[j], L′[k]} = M′[j]
M[k] = max{L[j], L[k]} ≤ max{L′[j], L′[k]} = M′[k]

so the result follows. □

Proposition 2. Shellsort and Shaker-sort are Shellsort-type algorithms.

Proof. It is clear that condition 1 is satisfied, and that 2 is satisfied by Shaker-sort. Condition 2 is also true for Shellsort, because N − h comparisons are necessary in an h-sort, even if only to check that the list is h-ordered. (This is Ω(N) when h < N/2.) So it remains to show that 3 holds.

Let L denote the k-ordered list at the beginning of a pass with increment h. Consider the following 2 by (N + k) array:

\begin{matrix}−∞ & ⋯ & −∞ & L[1] & ⋯ & L[N − k] & L[N − k + 1] & ⋯ & L[N] \\ L[1] & ⋯ & L[k] & L[k + 1] & ⋯ & L[N] & +∞ & ⋯ & +∞ \end{matrix}

Since L is k-ordered, each entry in the first row is less than or equal to the entry below it. Consider performing an h-sort on both rows or an h-shake on both rows. This has the same effect as performing it on the two copies of L within the array, because the −∞ and +∞ terms stay where they are. But this can be done by a series of compare-exchanges, so by Proposition 1, in the resulting array each entry of the first row is less than or equal to the entry below it. So the resulting list is k-ordered. □

Proposition 2 holds for Shellsort even if the method used to do the h-sort is not insertion sort, as long as conditions 1 and 2 still hold. This is because the result of an h-sort done by compare-exchanges at distance h is independent of the method. Shellsort-type algorithms have the following property, which will be used in proving our lower bound theorems.

Lemma 1. If a Shellsort-type algorithm is applied to a list which is h-ordered for all increments h ≥ β, where β is some fixed cutoff, then the passes with increments h ≥ β do nothing to the list.

Proof. By condition 3, the list will be h-ordered at the beginning of every pass, for each increment h ≥ β. So the compare-exchanges at distance h done during a pass with increment h ≥ β can’t do anything. □

If we dropped condition 3 from the definition of Shellsort-type algorithms, we could still prove Lemma 1 under the assumption that the increments were decreasing, because then at the beginning of a pass with increment h ≥ β, the list would be in its initial state, and would hence be h-ordered already.

3. The Frobenius Problem

Suppose a_{1}, …, a_{r} is a sequence of positive integers with gcd(a_{1}, …, a_{r}) = 1. Then every integer k can be expressed in the form n_{1}a_{1} + ⋯ + n_{r}a_{r} for some integers n_{1}, … n_{r}. We say k is expressible if the n_{i}’s can be chosen nonnegative, and inexpressible otherwise. It is not hard to show that if k is sufficiently positive, k is expressible. So a natural question to ask is, what is the largest inexpressible integer g = g(a_{1}, …, a_{r})? For example, suppose r = 2, a_{1} = 3, a_{2} = 5. Then the expressible integers are 0, 3, 5, 6, 8, 9, 10, 11, 12, …, so g = 7. (Note that if a_{1} consecutive integers are expressible, then all larger integers will be expressible.)

The problem of determining g is known as the Frobenius problem, because according to Brauer [1], Frobenius mentioned it in his lectures. Although the problem dates back to the nineteenth century [13], no general formula for g in terms of a_{1}, …, a_{r} has been found. The problem has been solved in some special cases, however. (See [12] for some of these.) For example, the following result is well-known. For a proof, see Exercise 21 in Section 5.2.1 of [6] or Example 1 on page 4 of [12]. Proposition 3. If gcd(a_{1}, a_{2}) = 1, g(a_{1}, a_{2}) = a_{1}a_{2} − a_{1} − a_{2}.

Our next result shows how knowledge of g can be useful in obtaining upper bounds on the time required to do an h-sort. Lemma 2. Let a_{1}, …, a_{r} be positive integers with greatest common divisor 1. The number of steps required to h-sort a list L[1], …, L[N] which is already (a_{1}h)-ordered, …, (a_{r}h)- ordered, is O(Ng), where g = g(a_{1}, …, a_{r}). Proof. By definition of g, every multiple of h greater than gh is a sum n_{1}(a_{1}h)+⋯+n_{r}(a_{r}h) for some n_{i} ≥ 0. Since L is (a_{1}h)-ordered, …, (a_{r}h)-ordered, it follows from transitivity that L[j − bh] ≤ L[j] for all b > g. So during the h-sort, inserting each element L[j] into its proper position in its sublist will involve moving it at most g steps of h. There are N elements to put into place, so the total number of steps required is at most Ng. □

4. Upper Bounds on the Worst Case

We now prove the best known upper bound for Shellsort, due to Pratt [10]. All logarithms in this paper are to the base 2 unless explicitly written otherwise. Proposition 4. If the numbers of the form 2^{a}3^{b} less than N are used in decreasing order as increments in Shellsort, then the worst case and average case running times are Θ(N(log N)^{2}). Proof. By the time the list is to be h-sorted, the list will have already been (2h)-sorted and (3h)-sorted, and it will still be (2h)-ordered and (3h)-ordered by condition 3 in the definition of Shellsort-type algorithms. So by Lemma 2, the h-sort requires only O(Ng(2, 3)) = O(N) steps. There are ⌈log_{2} N⌉ powers of 2 less than N and ⌈log_{3} N⌉ powers of 3 less than N, so there are at most ⌈log_{2} N⌉⌈log_{3} N⌉ = O((log N)^{2}) increments. Thus the total number of steps is always O(N(log N)^{2}).

By condition 2 in the definition of Shellsort-type algorithms, Ω(N) comparisons are required for each increment h < N/2. And there are Ω((log N)^{2}) increments less than N/2, since there are ⌈log_{2} \sqrt{N/2}⌉ powers of 2 less than \sqrt{N/2}, which may be paired with the ⌈log_{3} \sqrt{N/2}⌉ powers of 3 less than \sqrt{N/2}. So the total number of steps is Ω(N(log N)^{2}) also. □

Even though Pratt’s increment sequence gives the best known theoretical bound, it does not give the fastest algorithm in practice. (See [4] for some empirical results.) This may be because the other sequences actually have an average case better than Θ(N(log N)^{2}). Or it may be that N is not sufficiently large yet. But certainly one reason is that too many increments are being used. (There are Θ((log N)^{2}) of them.) In order to get reasonable running times in practice, it seems necessary to limit the number of increments to O(log N). So what time bounds can we get under this restriction? We shall see that the asymptotic worst case running time worsens as we reduce the number of increments we are allowed to use.

For now, we give upper bounds where the number of increments is restricted. A result very similar to the following (the special case m = Θ(log N)) was proved by Chazelle. (See Theorem 3 (non-uniform case) in [4].)

Theorem 1. For all m, there exists a sequence of at most m increments for which Shellsort requires O(mN^{1+O(1/\sqrt{m})}) steps in the worst case.

Proof. The proof is a generalization of the proof of Pratt’s result. If m ≥ (log N)^{2} we are done, since we can take the sequence of Proposition 4. Otherwise, pick the smallest integer α ≥ 3 such that ⌊log_{α−1} N⌋⌊log_{α} N⌋ ≤ m. There are at most m numbers of the form (α − 1)^{a}α^{b} less than N, so we may use them as increments, in decreasing order. As in the proof of Proposition 4, each h-sort will require O(Ng(α−1, α)) steps. By Proposition 3, this is O(Nα^{2}). Since there are at most m increments, the total running time will be O(mNα^{2}). Now m < (log N)^{2}, so by definition of α,

(\frac{log N}{log α} )^{2} = Θ(m)
α = N^{Θ(1/\sqrt{m})}
O(mNα^{2}) = O(mN^{1+O(1/\sqrt{m})})

as desired. □

Theorem 2. There is a constant c_{1} such that for all sufficiently large N and all m ≤ (log N/ log log N)^{2}, there exists a sequence of at most m increments for which Shellsort requires at most N^{1+c_{1}/\sqrt{m}} steps.

Proof. By Theorem 1, there are constants c, c′ such that we can find a sequence of at most m increments for which Shellsort requires at most cmN^{1+c′/\sqrt{m}} steps. But for sufficiently large N,

log N^{1/\sqrt{m}} = \frac{log N}{\sqrt{m}} ≥ log log N ≥ log c

and

log N^{2/\sqrt{m}} ≥ 2 log log N ≥ log m

so

N^{1+(3+c′)/\sqrt{m}} ≥ cmN^{1+c′/\sqrt{m}}

and we may take c_{1} = 3 + c′. □

In contrast with Proposition 4, the sequences produced by Theorems 1 and 2 are nonuniform. (The sequences depend on α which depends on N.) But it should be possible to obtain the same upper bounds for uniform sequences by generalizing Theorem 3 in [4], by choosing the sequence of primes a_{1}, a_{2}, a_{3}, … (of the proof there) with an appropriate rate of growth (not necessarily exponential).

5. Inversions

An inversion in a list L[1], …, L[N] is an ordered pair (i, j) where i < j and L[i] > L[j]. The number of inversions in a list is a measure of how much work must be done to sort it. For example, an already sorted list has no inversions, a list of length N in reverse order (such as N, N − 1, …, 1) has N(N − 1)/2 inversions, and a random list of length N is expected to have N(N − 1)/4 inversions, since each of the N(N − 1)/2 pairs (i, j) with i < j is an inversion with probability 1/2.

Counting inversions is especially useful when analyzing algorithms which sort by exchanges because of the following proposition:

Proposition 5. If i < j, exchanging elements L[i] and L[j] in a list changes the number of inversions by less than 2(j − i).

Proof. We need only consider the pairs which involve either i or j. If k < i, then (k, i) is an inversion in the new list iff (k, j) was an inversion in the original list, and (k, j) is an inversion in the new list iff (k, i) was an inversion in the original list. So there is no change in the number of inversions involving k where k < i. Similarly we need not consider the pairs involving k where k > j. The only pairs that remain are (i, k) and (k, j) for i < k < j, and (i, j) itself. But there are only 2(j − i) − 1 of these. □

We also need a result about inversions of a sublist. In proving this we will use the following inequality:

Lemma 3. If k ≥ 2 and 0 ≤ b_{1}, …, b_{k} ≤ t, then there exists s, 1 ≤ s ≤ k − 1, such that b_{s}(t − b_{s+1}) ≥ b_{1}(t − b_{k})/4.

Proof. Without loss of generality we may replace b_{1} with t. This can only shrink the set of s which satisfy the inequality. Similarly we may assume b_{k} = 0. Choose the largest s such that b_{s} ≥ t/2, so 1 ≤ s ≤ k − 1. Then b_{s+1} < t/2, so b_{s}(t − b_{s+1}) ≥ t^{2}/4 = b_{1}(t − b_{k})/4. □

The following result says (roughly) that in a list of N zeros and ones where a fraction p of the pairs are inversions, one can find a sublist of given length n ≤ N where at least a fraction p/256 of the pairs are inversions.

Proposition 6. If 2 ≤ n ≤ N and L[1], …, L[N] is a list of zeros and ones with I inversions, then some sublist L[k], …, L[k + n − 1] of length n has at least \frac{n^{2}I}{256N^{2}} inversions.

Proof. Let t = ⌊n/2⌋ ≥ 1 and q = ⌈N/t⌉ ≥ 2. Then t ≥ n/4, and q ≤ 2N/t ≤ 8N/n. Make a list of length N′ = qt with I inversions by appending N′ − N ones to the end of L. Divide this list into q blocks B_{i} of length t. Let b_{i} be the number of ones in B_{i}, and let n_{i} be the number of inversions within B_{i}. Then

I = ∑_{i<j} b_{i}(t − b_{j}) + ∑_{i=1}^{q} n_{i},

since the first sum counts the number of inversions involving a pair of elements from different blocks. If n_{i} ≥ I/2q for some i, then any sublist of length n containing the part of B_{i} belonging to the original list L will have at least

\frac{I}{2q} ≥ \frac{I}{2} (\frac{n}{8N} ) ≥ \frac{n^{2}I}{256N^{2}}

inversions. Otherwise

∑_{i<j} b_{i}(t − b_{j}) = I − ∑_{i=1}^{q} n_{i} ≥ I − q (\frac{I}{2q} ) = \frac{I}{2},

so for some i < j,

b_{i}(t − b_{j}) ≥ \frac{I/2}{q(q − 1)/2} ≥ \frac{I}{q^{2}} ≥ \frac{n^{2}I}{64N^{2}} .

By the previous lemma applied to b_{i}, b_{i+1}, …, b_{j}, we get b_{s}(t − b_{s+1}) ≥ \frac{n^{2}I}{256N^{2}} for some s. So any sublist of length n containing the part of B_{s} ∪ B_{s+1} belonging to the original list L will have at least \frac{n^{2}I}{256N^{2}} inversions. □

6. A Geometric Interpretation of the Frobenius Problem

Throughout the next three sections, a_{1}, …, a_{r} will be a fixed sequence of integers with 2 ≤ a_{1} ≤ ⋯ ≤ a_{r} and gcd(a_{1}, …, a_{r}) = 1. (So r ≥ 2.) Define a = a_{1} + ⋯ + a_{r}. We let g = g(a_{1}, …, a_{r}) as in Section 3. We will also be interested in a related function ψ(x), defined as the number of (nonnegative) expressible integers less than or equal to x. In our example r = 2, a_{1} = 3, and a_{2} = 5 from Section 3, we have ψ(6) = 4, for instance.

To get information about ψ(x) and g, we will express ψ(x) as a volume of a certain geometrical figure, and approximate the figure. First we need a few definitions. Label each point (x_{1}, …, x_{r}) ∈ ℝ^{r} with the integer a_{1}⌊x_{1}⌋ + ⋯ + a_{r}⌊x_{r}⌋. Let L be the set of lattice points with label zero. So L is a subgroup of ℤ^{r} isomorphic (as an abstract group) to ℤ^{r−1}. For x ≥ 0, define regions

P = { (x_{1}, …, x_{r}) ∈ ℝ^{r} | x_{i} ≥ 0 for all i }
S = L + P =^{def} { v + w | v ∈ L, w ∈ P }
Q(x) = { (x_{1}, …, x_{r}) ∈ ℝ^{r} | 0 ≤ a_{1}x_{1} + ⋯ a_{r}x_{r} ≤ x }
S(x) = { v ∈ S with label at most x }
T (x) = S ∩ Q(x) = { (x_{1}, …, x_{r}) ∈ S | a_{1}x_{1} + ⋯ a_{r}x_{r} ≤ x }

Lemma 4. The region S is the set of points with expressible label.

Proof. If w ∈ P the label of w is obviously expressible. But for any v ∈ L, w and v + w have the same label. Thus every point in S has expressible label. Conversely, if w = (w_{1}, …, w_{r}) has an expressible label, equal to a_{1}n_{1} + ⋯ + a_{r}n_{r}, where n_{i} ∈ ℤ, n_{i} ≥ 0, then w ∈ (v + P), where v = (⌊w_{1}⌋ − n_{1}, …, ⌊w_{r}⌋ − n_{r}) ∈ L. □

We think of T (x) as an approximation to S(x), because of the following lemma. Recall that a = a_{1} + ⋯ + a_{r}. Think of a as an error term. We will later impose conditions that make a small compared to other relevant quantities.

Lemma 5. For all x ≥ 0, T (x) ⊆ S(x) ⊆ T (x + a).

Proof. The first inclusion follows, because if a_{1}x_{1} + ⋯ + a_{r}x_{r} ≤ x, then a_{1}⌊x_{1}⌋ + ⋯ + a_{r}⌊x_{r}⌋ ≤ x. The second inclusion follows, because if a_{1}⌊x_{1}⌋ + ⋯ + a_{r}⌊x_{r}⌋ ≤ x, then a_{1}x_{1} + ⋯ + a_{r}x_{r} ≤ x + a. □

Now for any region U invariant under translation by elements of L (such as S, Q(x), S(x), or T (x)), we define the volume of U per point of L as the r-dimensional volume of any measurable region U_{0} such that U is the disjoint union of the translates v + U_{0} where v ∈ L. (This is independent of the choice of U_{0}.)

Lemma 6. The volume of Q(x) per point of L is x.

Proof. If {e_{1}, …, e_{r−1}} is a basis for L (as a ℤ-module), and e_{r} is any lattice point in ℤ^{r} with label 1, then {e_{1}, …, e_{r−1}, e_{r}} is a basis for ℤ^{r}, because for each v ∈ ℤ^{r} there is a unique integer k such that v − ke_{r} ∈ L, namely the label of v. So the change of coordinates to {e_{1}, …, e_{r}} has determinant ±1, and hence preserves volumes. Under this transformation, L becomes { (x_{1}, …, x_{r−1}, 0)} ∈ ℤ^{r} }, and Q(x) becomes { (x_{1}, …, x_{r}) ∈ ℝ^{r} | 0 ≤ x_{r} ≤ x }, so the result is now clear. □

Lemma 7. The volume of S(x) per point of L is ψ(x).

Proof. For each expressible integer k ≤ x, pick a cube [n_{1}, n_{1} + 1) × ⋯ × [n_{r}, n_{r} + 1) where (n_{1}, …, n_{r}) is a lattice point with label k. Let U_{0} be their union. Then S(x) is the disjoint union of (v + U_{0}) for v ∈ L (because of Lemma 4), and the volume of U_{0} is clearly ψ(x). □

To approximate ψ(x), we define Ψ(x) as the volume of T (x) per point of L. To check this is well-defined, we must exhibit a measurable region U_{0} such that T (x) is the disjoint union of the translates of U_{0}. First, we can make L into an ordered group by identifying it with a subgroup of the additive group of ℝ isomorphic to ℤ^{r−1}. Then for v ∈ L, we can define

P_{v} = (v + P) \ ⋃_{\begin{matrix}w∈L \\ w<v \end{matrix}}(w + P).

Each z ∈ S is in finitely many w + P for w ∈ L, so S is the disjoint union of the P_{v}. Also P_{v} = v + P_{0} for each v ∈ L. Thus T (x) is the disjoint union of the translates of P_{0} ∩ Q(x), so Ψ(x) is the volume of P_{0} ∩ Q(x).

The advantage of working with Ψ(x) instead of ψ(x) is that the region T (x) is much simpler than S(x). We shall see that whereas ψ(x) is a step function (stepping up 1 at each expressible integer), Ψ(x) is continuous and even differentiable.

We also want to approximate g. By Lemma 4, g is the supremum of the labels of points in ℝ^{r} \ S. So we define G as the supremum of a_{1}x_{1} + ⋯ + a_{r}x_{r} for (x_{1}, …, x_{r}) ∈ ℝ^{r} \ S.

Lemma 8. G = g + a.

Proof. By Lemma 4, ℝ^{r} \ S is the union of the cubes [n_{1}, n_{1} + 1) × ⋯ × [n_{r}, n_{r} + 1) where (n_{1}, …, n_{r}) ranges over lattice points with inexpressible label. So we maximize a_{1}x_{1}+⋯ a_{r}x_{r} in ℝ^{r} \ S by picking a lattice point (n_{1}, …, n_{r}) with maximal inexpressible label and looking at the value a_{1}x_{1} + ⋯ + a_{r}x_{r} at (n_{1} + ϵ, …, n_{r} + ϵ) as ϵ approaches 1. Thus we get G = g + a. □

7. Properties of Ψ(x) and G

We will derive a large number of results about Ψ(x) and G, and use these to get information about ψ(x) and g. But first we need a lemma.

Lemma 9. If r ≥ 2, (1 − 1/r)^{r} ≥ 1/4.

Proof. For r ≥ 2 we can apply the arithmetic-geometric mean inequality to get

1 − 1/(r + 1) = \frac{1 + \overbrace{(1 − 1/r) + ⋯ + (1 − 1/r)}^{r terms}}{r + 1} ≥ \sqrt[r+1]{1 · \underbrace{(1 − 1/r) ⋯ (1 − 1/r)}_{r terms}}.

So (1 − 1/(r + 1))^{r+1} ≥ (1 − 1/r)^{r} for all r ≥ 2. Thus

(1 − 1/r)^{r} ≥ (1 − 1/(r − 1))^{r−1} ≥ ⋯ ≥ (1 − 1/2)^{2} = 1/4.
□

In the following proposition, the first six properties are proved using geometrical arguments. The rest are simply corollaries of these six.

Proposition 7.

(1) Ψ(x) is increasing for x ≥ 0

(2) Ψ(x) ≤ \frac{x^{r}}{r!a_{1}a_{2}···a_{r}} with equality for sufficiently small x ≥ 0

(3) Ψ(G) ≤ G/2

(4) If x ≥ y ≥ 0, then Ψ(x) − Ψ(y) ≤ x − y, and equality holds if x ≥ y ≥ G

(5) Ψ is differentiable

(6) Ψ′(αx) ≥ α^{r−1}Ψ′(x) for x ≥ 0, 0 ≤ α ≤ 1

(7) |Ψ(x) − Ψ(y)| ≥ |x − y| for all x, y ≥ 0.

(8) 0 ≤ Ψ′(x) ≤ 1 for all x ≥ 0

(9) Ψ′(x) = 1 for x ≥ G

(10) 0 ≤ Ψ(x) ≤ x for all x ≥ 0

(11) x − Ψ(x) is increasing for x ≥ 0

(12) Ψ(x) ≥ xΨ′(x)/r

(13) Ψ(x)/x^{r} is decreasing for x > 0

(14) Ψ(αx) ≥ α^{r}Ψ(x) for x ≥ 0, 0 ≤ α ≤ 1

(15) Ψ(G) ≥ G/r

(16) G ≥ a_{1}^{1+1/r}

(17) Ψ(G − Ψ(G)) ≥ G/8r

Proof. If x ≥ y ≥ 0, then T (x) ⊇ T (y), so Ψ(x) ≥ Ψ(y). So (1) holds. For all x ≥ 0, P_{0} ∩ Q(x) is contained in the r-dimensional simplex P ∩ Q(x), and they are equal for sufficiently small x, by definition of P_{0}. The lengths of the r edges of this simplex meeting orthogonally at 0 are x/a_{1}, …, x/a_{r}, so the volume is (1/r!)(x/a_{1}) ⋯ (x/a_{r}). Thus (2) holds.

Statement (3) is analogous to the result that ψ(g) ≤ (g + 1)/2 (due to Nijenhuis and Wilf [8]), which is proved by noting that if k is expressible, g − k cannot be expressible. The proof is analogous as well, although it is harder to state. By definition of G, we can pick v = (v_{1}, …, v_{r}) ∈ ℝ^{r} \ int(S) with a_{1}v_{1} + ⋯ + a_{r}v_{r} = G. The interior int(S) of S is closed under addition, since int(S) = L + int(P), where L and int(P) are closed under addition.

If the union int(T (G)) ∪ (v − int(T (G))) inside Q(G) were not disjoint, then v would be a sum of two vectors in int(S), which contradicts v ∉ int(S). Thus this union is disjoint, and by taking the volume per point of L, we deduce from the definition of Ψ(G) and from Lemma 6 that 2Ψ(G) ≤ G. (Note that as far as volumes are concerned, it does not matter that we have int(T (G)) instead of T (G).) Thus Ψ(G) ≤ G/2.

If x ≥ y ≥ 0, then T (x) \ T (y) ⊆ Q(x) \ Q(y), by their definitions, and equality holds for x ≥ y ≥ G, by definition of G. By taking the volume per point of both sides and applying Lemma 6, we get (4).

It is clear from the geometry that Ψ is differentiable and that Ψ′(x) is proportional to the (r − 1)-dimensional volume per point of L of the face of T (x) contained in the hyperplane a_{1}x_{1} +…+a_{r}x_{r} = x. (This is the part of T (x) that grows with x.) Since T (x) is the disjoint union of the translates of P_{0} ∩ Q(x), this can also be expressed as the (r − 1)-dimensional volume of the face F(x) of P_{0} ∩ Q(x) contained in the hyperplane a_{1}x_{1} + … + a_{r}x_{r} = x. If 0 ≤ α ≤ 1, and v ∈ P_{0} ∩ Q(x), then αv ∈ P_{0} ∩ Q(αx), so αF(x) ⊆ F(αx). Taking (r − 1)-dimensional volumes, we get α^{r−1}Ψ′(x) ≤ Ψ′(αx).

Now (7), (8), and (9) follow from (1), (4), and (5). By (2), Ψ(0) = 0, so (10) and (11) follow from (8). Integrating (6) with respect to α from 0 to 1 yields (1/x)(Ψ(x) − Ψ(0)) ≥ (1/r)Ψ′(x), so (12) holds. Since

\frac{d}{dx} (\frac{Ψ(x)}{x^{r}}) = \frac{Ψ′(x)}{x^{r}} − \frac{rΨ(x)}{x^{r+1}} = −\frac{r}{x^{r+1}} (Ψ(x) − \frac{xΨ′(x)}{r}) ,

(13) follows from (12). Then (14) follows from (13). Set x = G in (9) and (12) to deduce (15). By (15) and (2),

\frac{G}{r} ≤ Ψ(G) ≤ \frac{G^{r}}{r!a_{1}a_{2} ⋯ a_{r}} ≤ \frac{G^{r}}{ra_{1}^{r}} ,

so G ≥ a_{1}^{1+1/(r−1)}. This implies (16).

By (3), G − Ψ(G) ≥ G/2. So if Ψ(G − Ψ(G)) ≥ (G − Ψ(G))/r, (17) follows. Otherwise, by (15), the continuous function Φ(x) − x/r changes sign in [G − Ψ(G), G], so Ψ(H) = H/r for some H ∈ [G − Ψ(G), G]. Then

G − Ψ(G) ≥ H − Ψ(H)  (by (11))
Ψ(G − Ψ(G)) ≥ Ψ(H − Ψ(H))  (by (1))
= Ψ((1 − 1/r)H)
≥ (1 − 1/r)^{r}Ψ(H) (by (14))
= (1 − 1/r)^{r}H/r
≥ H/4r,

by Lemma 9. But H ≥ G − Ψ(G) ≥ G/2, so Ψ(G − Ψ(G)) ≥ G/8r. □

Our goal is to produce a result similar to (17) for ψ(x) and g.

Lemma 10. |ψ(x) − Ψ(x)| ≤ a.

Proof. Taking volumes per point of L in Lemma 5 gives Ψ(x) ≤ ψ(x) ≤ Ψ(x + a). But Ψ(x + a) ≤ Ψ(x) + a, by (4) in Proposition 7, so the result follows. □

Lemma 11. ψ(g − ψ(g)) ≥ G/8r − 4a.

Proof. First we will apply Lemma 10, (7) from Proposition 7, and Lemma 8 repeatedly to show ψ(g − ψ(g)) is close to Ψ(G − Ψ(G)). From |ψ(g) − Ψ(g)| ≤ a and |Ψ(g) − Ψ(G)| ≤ |g − G| = a, we get |ψ(g) − Ψ(G)| ≤ 2a. Similarly from |ψ(g − ψ(g)) − Ψ(g − ψ(g))| ≤ a and

|Ψ(g − ψ(g)) − Ψ(G − Ψ(G))| ≤ |(g − ψ(g)) − (G − Ψ(G)|
≤ |g − G| + |ψ(G) − Ψ(G)|
≤ a + 2a
= 3a,

we get |ψ(g − ψ(g)) − Ψ(G − Ψ(G))| ≤ 4a. Thus

ψ(g − ψ(g)) ≥ Ψ(G − Ψ(G)) − 4a ≥ G/8r − 4a,

by (17) in Proposition 7. □

8. Inversions in the Frobenius Pattern

The Frobenius pattern is the list L[0], …, L[g], where L[i] is 1 if i is expressible, and 0 otherwise. In our example r = 2, a_{1} = 3, and a_{2} = 5 from Section 3, the Frobenius pattern is

\begin{matrix}i & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ L[i] & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \end{matrix}

Lemma 12. The Frobenius pattern is a_{1}-ordered, …, a_{r}-ordered.

Proof. Suppose 0 ≤ k < k +a_{i} ≤ g. If L[k] = 1, then k is expressible, so k +a_{i} is expressible, and L[k + a_{i}] = 1. So L[k] ≤ L[k + a_{i}] in any case. □

Lemma 13. The Frobenius pattern has at least (ψ(g − ψ(g)))^{2} inversions.

Proof. The Frobenius pattern has ψ(g) ones and g + 1 − ψ(g) zeros. There are ψ(g − ψ(g)) ones in the first g + 1 − ψ(g) positions (where the zeros belong), so there must also be ψ(g − ψ(g)) zeros in the last ψ(g) positions (where the ones belong). Each of these ones occurs before each of these zeros, so we get (ψ(g − ψ(g)))^{2} inversions. □

The next lemma says roughly that the fraction of pairs in the Frobenius pattern which are inversions is at least Ω(r^{−2}). To prove it, we need a few hypotheses to guarantee that a will be small compared to G/r.

Lemma 14. If a_{r} ≤ 2a_{1} and a_{1} ≥ r^{9r}, then the Frobenius pattern has length N ≥ \frac{1}{2}a_{1}^{1+1/r} and has at least N^{2}/256r^{2} inversions.

Proof. First,

\frac{G}{64r} ≥ \frac{a_{1}^{1+1/r}}{64r}  (by (16) in Proposition 7)
≥ \frac{r^{9}a_{1}}{64r}
≥ 2ra_{1}  (since r ≥ 2)
= \overbrace{2a_{1} + ⋯ + 2a_{1}}^{r terms}
≥ a_{1} + ⋯ a_{r},

so a ≤ G/64r. Thus

N = (G − a) + 1 (by Lemma 8)
≥ G/2
≥ \frac{1}{2}a_{1}^{1+1/r}

by (16) in Proposition 7. Also, by Lemma 11,

ψ(g − ψ(g)) ≥ G/8r − 4a ≥ G/8r − 4(G/64r) = G/16r ≥ N/16r,

since G ≥ G−a+1 = N. By Lemma 13, the Frobenius pattern contains at least (N/16r)^{2} = N^{2}/256r^{2} inversions. □

9. Making Lists with Lots of Inversions

In this section, we show how to use Frobenius patterns to build other lists which are hordered for various h’s but which still have many inversions. Our first lemma is similar in spirit to Exercise 16 in Section 5.2.1 of [6].

Lemma 15. If N, h ≥ 2, there exists an h-ordered list of N zeros and ones having at least N^{2}/256 inversions.

Proof. If N < h then the list of length N in reverse order is h-ordered and has N(N −1)/2 ≥ N^{2}/4 inversions. Otherwise let q = ⌊N/h⌋, so q ≥ N/2h. For 0 ≤ i ≤ h − 1, let

L[i] = \begin{cases}1 & if (i mod h) < h/2, \\ 0 & otherwise. \end{cases}

For 1 ≤ j ≤ k ≤ q, we get ⌈h/2⌉⌊h/2⌋ inversions by pairing the ⌈h/2⌉ ones in the j^{th} block of h elements with the ⌊h/2⌋ zeros in the k^{th} block of h elements, so there are at least \frac{1}{2}q(q + 1)⌈h/2⌉⌊h/2⌋ inversions in L. But \frac{1}{2}q(q + 1) ≥ q^{2}/2 ≥ N^{2}/8h^{2} and ⌈h/2⌉⌊h/2⌋ ≥ (h/2)(h/4), so this is at least N^{2}/256 inversions. □

Lemma 16. If a_{1}, …, a_{r} are arbitrary integers satisfying 2 ≤ a_{1} ≤ ⋯ ≤ a_{r} and a_{1} ≥ r^{9r}, then there exists N ≥ \frac{1}{2}a_{1}^{1+1/r} and a list of N zeros and ones which is a_{1}-ordered,…,a_{r}- ordered, and which has at least N^{2}/256r^{2} inversions.

Proof. We can assume a_{r} < 2a_{1}, because otherwise we can replace each a_{i} with b_{i} = a_{1} + (a_{i} mod a_{1}) (and then order the b_{i}’s). A list which is b_{i}-ordered for all i will be a_{i}-ordered for all i, since a_{1} = b_{1} and each other a_{i} is b_{i} plus some nonnegative multiple of a_{1}.

Let h = gcd(a_{1}, …, a_{r}). If h = 1, the result follows from Lemma 14. If h ≥ 2, then for any N ≥ 2 there is an h-ordered list with at least N^{2}/256 inversions, by Lemma 15. This list is a_{1}-ordered,…,a_{r}-ordered, so we are done. □

Lemma 17. If 2 ≤ a_{1} ≤ ⋯ ≤ a_{r}, a_{1} ≥ r^{9r}, and 2 ≤ l ≤ \frac{1}{2}a_{1}^{1+1/r}, there exists a list of l zeros and ones which is a_{1}-ordered,…,a_{r}-ordered, and which has at least 2^{−16}l^{2}/r^{2} inversions.

Proof. By the previous lemma, for some N ≥ l there is a list of N zeros and ones which is a_{1}-ordered,…,a_{r}-ordered, and which has at least N^{2}/256r^{2} inversions. By Proposition 6, we can take a sublist of length l having at least (l^{2}/256N^{2})(N^{2}/256r^{2}) = 2^{−16}l^{2}/r^{2} inversions, and this sublist is also a_{1}-ordered,…,a_{r}-ordered. □

Everything so far has been building up to the following lemma, which is the only result from the past four sections which we use later.

Lemma 18. If 2 ≤ a_{1} ≤ ⋯ ≤ a_{r}, a_{1} ≥ r^{9r}, 2 ≤ l ≤ \frac{1}{2}a_{1}^{1+1/r}, and N ≥ l, then there exists a list of length N which is a_{1}-ordered,…,a_{r}-ordered, and h-ordered for all h ≥ l, and which has at least 2^{−17}Nl/r^{2} inversions.

Proof. Let L[0], L[1], …, L[l − 1] be the list of l zeros and ones given by the previous lemma. For 0 ≤ i ≤ N − 1, define L′[i] = 10⌊i/l⌋ + L[i mod l]. Then L′ contains ⌊N/l⌋ ≥ N/2l copies of L (with entries displaced by various multiples of 10), so L′ contains at least (N/2l)(2^{−16}l^{2}/r^{2}) = 2^{−17}Nl/r^{2} inversions. Also L′ is clearly a_{1}-ordered,…,a_{r}-ordered, and h-ordered for all h ≥ l. □

10. Lower Bounds on the Worst Case

We are now ready to prove a lower bound corresponding to the upper bound of Theorem 2. The situation is as follows: we are sorting a list of length N using a Shellsort-type algorithm. Let c_{2} = 1/432, and let m be the number of increments less than N/2.

Theorem 3. If N is sufficiently large, there is a list of length N on which the algorithm must perform at least N^{1+c_{2}/\sqrt{m}} compare-exchange operations.

Before proving the theorem, let us remark that if we defined m as the total number of increments, we would be proving a weaker result. So the theorem would still hold. But we will need the stronger version when we prove our next theorem.

Proof. If N^{c_{2}/\sqrt{m}} < \sqrt{log N}, then

N^{1+c_{2}/\sqrt{m}} < N\sqrt{log N}
≤ log N!,

for sufficiently large N. We would then be done, since this is the information-theoretic lower bound on the number of comparisons required. So we may assume

(1)  N^{c_{2}/\sqrt{m}} ≥ \sqrt{log N}
\frac{c_{2}}{\sqrt{m}} log N ≥ \frac{1}{2} log log N

(2)  m ≤ (\frac{log N}{log log N} )^{2} ,

since c_{2} < 1/2.

For k ≥ 0, define β_{k} = N^{\frac{1}{2} + \frac{k}{72\sqrt{m}}}. If k ∈ ℤ and 0 ≤ k ≤ ⌈18\sqrt{m}⌉, then

k ≤ 18\sqrt{m} + 1 ≤ 24\sqrt{m}
β_{k} ≤ N^{\frac{1}{2} + \frac{24\sqrt{m}}{72\sqrt{m}}} = N^{5/6} ≤ N/2,

provided N is sufficiently large. So we have at least 18\sqrt{m} disjoint intervals [β_{i}, β_{i+1}) with β_{i+1} ≤ N/2, which together contain at most m increments. Thus some interval [β_{i}, β_{i+1}) contains r increments a_{1} ≤ ⋯ ≤ a_{r}, where

(3)  r ≤ \frac{m}{18\sqrt{m}} = \frac{\sqrt{m}}{18}.

By (2),

(4)  r ≤ \frac{1}{18} (\frac{log N}{log log N} )
(5)  r ≤ log N,

for sufficiently large N.

The next two paragraphs are devoted to showing that the conditions of Lemma 18 hold. We have

(6)  a_{1} ≥ β_{i} ≥ \sqrt{N},

so

log a_{1} ≥ \frac{1}{2} log N
= 9 [\frac{1}{18} (\frac{log N}{log log N} )] (log log N)
≥ 9r log r,

by (4) and (5). Thus a_{1} ≥ r^{9r}.

Let l = ⌈β_{i+1}⌉. Then 2 ≤ l ≤ N, and

l ≤ 2β_{i+1}
= 2N^{\frac{1}{72\sqrt{m}}} β_{i}
≤ (\frac{1}{2}\sqrt{log N}) N^{\frac{1}{72\sqrt{m}}} β_{i}  (for sufficiently large N)
≤ \frac{1}{2}N^{c_{2}/\sqrt{m}}N^{\frac{1}{72\sqrt{m}}} β_{i}  (by (1))
≤ \frac{1}{2}N^{9/\sqrt{m}}β_{i}  (since c_{2} + 1/72 ≤ 9)
≤ \frac{1}{2}a_{1}^{18/\sqrt{m}}a_{1}  (by (6))
≤ \frac{1}{2}a_{1}^{1+1/r}  (by (3)).

So we may apply Lemma 18 to obtain a list L of length N which is a_{1}-ordered, …, a_{r}-ordered, and h-ordered for all h ≥ l, and which has at least 2^{−17}Nl/r^{2} inversions. In particular, L is h-ordered for each increment h ≥ β_{i}. By Lemma 1, no exchanges are done during the passes corresponding to these large increments, so the burden of removing the inversions is laid upon the passes with increments less than β_{i}. By Proposition 5, each exchange during those passes reduces the number of inversions by less than 2β_{i}. To reduce this number to zero (to sort the list), the number of exchanges needed is at least

\frac{1}{2β_{i}} (\frac{Nl}{2^{17}r^{2}} ) ≥ \frac{Nβ_{i+1}}{2^{18}β_{i}(log N)^{2}}  (by (5))

= \frac{N · N^{\frac{1}{72\sqrt{m}}}}{2^{18}(log N)^{2}}
= \frac{N^{1+c_{2}/\sqrt{m}}N^{5c_{2}/\sqrt{m}}}{2^{18}(log N)^{2}}  (since 6c_{2} = 1/72)
≥ \frac{N^{1+c_{2}/\sqrt{m}}(\sqrt{log N})^{5}}{2^{18}(log N)^{2}}  (by (1))
≥ N^{1+c_{2}/\sqrt{m}},

for sufficiently large N. □

The next theorem follows quite easily.

Theorem 4. Any Shellsort-type algorithm makes Ω(N(log N/ log log N)^{2}) comparisons in the worst case.

Proof. As in Theorem 3, let m be the number of increments less than N/2. If

m > \frac{c_{2}^{2}}{4} (\frac{log N}{log log N} )^{2} ,

then we are done, since each pass with increment h < N/2 makes Ω(N) comparisons. Otherwise

\sqrt{m} ≤ \frac{c_{2}}{2} (\frac{log N}{log log N} )
\frac{c_{2}}{\sqrt{m}} log N ≥ 2 log log N
N^{c_{2}/\sqrt{m}} ≥ (log N)^{2}
N^{1+c_{2}/\sqrt{m}} ≥ N(log N)^{2},

so by Theorem 3, at least N(log N)^{2} comparisons will be made in the worst case, for sufficiently large N. □

For the particular case of Shaker-sort, we can prove an even stronger result for some increment sequences.

Theorem 5. If h_{j} = Θ(α^{j}) for some fixed real α > 1, and the increments h_{j} less than N are used in any order in Shaker-sort, then Ω(N^{2}) steps are required in the worst case.

Proof. Let h_{m} be the largest increment used and let a_{i} = h_{m−r+i}, 1 ≤ i ≤ r, be the r largest increments in increasing order. (We’ll choose r later.) For fixed r, a_{1} = Θ(N/α^{r}) = Θ(N), so N ≤ \frac{1}{2}a_{1}^{1+1/r} for large N. Thus by Lemma 18 with l = N, there exists a list L of length N which is a_{1}-ordered, …, a_{r}-ordered and which has at least 2^{−17}N^{2}/r^{2} inversions.

An h-shake involves at most 2N compare-exchanges at a distance of h, so by Proposition 5, the increments h_{1}, …, h_{m−r} can remove at most

4N(h_{1} + h_{2} + ⋯ + h_{m−r}) = Θ(N(α + α^{2} + ⋯ + α^{m−r}))
= Θ(Nα^{m−r})
= Θ(α^{−r}N^{2})

inversions. Pick r large enough that this is less than or equal to 2^{−18}N^{2}/r^{2}. Then when Shaker-sort is applied to L, the increments a_{1}, …, a_{r} do no work (by Lemma 1), so after all the h-shakes, at least

2^{−17}N^{2}/r^{2} − 2^{−18}N^{2}/r^{2} = 2^{−18}N^{2}/r^{2}

inversions remain to removed by the insertion sort at the end, and this will take at least 2^{−18}N^{2}/r^{2} steps. Since r is independent of N, this is Ω(N^{2}). □

11. Unsolved Problems

We now mention some things we would have liked to prove. It would be nice to have a tight bound on the worst case performance. There is still a small gap between our upper and lower bounds, when the number of increments exceeds (log N/ log log N)^{2}, and when the number of increments is unrestricted. We know that the complexity of Shellsort, for instance is Ω(N(log N/ log log N)^{2}) and O(N(log N)^{2}). What is the actual complexity? Probably the answer is Θ(N(log N)^{2}), so Pratt’s increments are the best asymptotically, but it seems impossible to obtain this lower bound simply by refining the techniques used above. Making more careful estimates seems only to improve the constants.

An even more interesting problem, which we have not considered at all in this paper, is the determination of the average case behavior of Shellsort or Shaker-sort. This is irrelevant when constructing sorting networks, but it is important when using either method as a general-purpose sorting algorithm. Virtually nothing is known about the average case. The only increment sequences for which the average running time of Shellsort has been calculated are (h, k, 1) (see [17]), and sequences in which each increment is a multiple of the next (see the Corollary to Theorem H in Section 5.2.1 of [6]). None of our results rule out an O(N log N) average case Shellsort or Shaker-sort, although perhaps they make it a little less likely.

References

[1] Brauer, A., On a Problem of Partitions, Amer. J. Math. 64 (1942), 299–312.

[2] Cypher, R., A Lower Bound on the Size of Shellsort Sorting Networks, Proceedings of the 1989 International Conference on Parallel Processing, 58–63.

[3] Hibbard, T., An Empirical Study of Minimal Storage Sorting, Comm. of the ACM 6, no. 5 (May 1963), 206–213.

[4] Incerpi, J. and R. Sedgewick, Improved Upper Bounds on Shellsort, J. Comput. System. Sci. 31, no. 2 (1985), 210–224.

[5] Incerpi, J. and R. Sedgewick, Practical Variations on Shellsort, Inform. Process. Lett. 26 (1987), 37–43.

[6] Knuth, D., “The Art of Computer Programming. 3. Sorting and Searching”, Addison-Wesley, Reading, Mass., 1973.

[7] Lazarus, R. and R. Frank, A High-Speed Sorting Procedure, Comm. of the ACM 3, no. 1 (1960), 20–22.

[8] Nijenhuis, A. and H. Wilf, Representations of Integers by Linear Forms in Nonnegative Integers, J. Number Theory 4 (1972), 98–106.

[9] Papernov, A. and G. Stasevich, A Method of Information Sorting in Computer Memories, Problems of Inform. Transmission 1, no. 3 (1965), 63–75.

[10] Pratt, V., “Shellsort and Sorting Networks”, Garland Publishing, New York, 1979. (Originally presented as the author’s Ph. D. thesis, Stanford University, 1971.)

[11] Sedgewick, R., A New Upper Bound for Shellsort, J. of Algorithms 2 (1986), 159–173.

[12] Selmer, E., On the Linear Diophantine Problem of Frobenius, J. reine angew. Math. 294 (1977), 1–17.

[13] Sharp, W. J. Curran, Solution to Problem 7382 (Mathematics) proposed by J. J. Sylvester, Ed. Times 41 (1884), London.

[14] Shell, D., A High-Speed Sorting Procedure, Comm. of the ACM 2, no. 7 (1959), 30–32.

[15] Weiss, M. and R. Sedgewick, Bad Cases for Shaker Sort, Inform. Process. Lett. 28 (1988), 133–136.

[16] Weiss, M. and R. Sedgewick, Tight Lower Bounds for Shellsort, J. of Algorithms 11 (1990), 242–251.

[17] Yao, A., An Analysis of (h, k, 1)-Shellsort, J. of Algorithms 1 (1980), 14–50.

Department of Mathematics, University of California at Berkeley, Berkeley, CA 94720

  E-mail address: poonen@math.berkeley.edu