3

I am looking for worst case of Shell sort. According to this, the worst case is O(N^3/2) but here, it is claimed that the worst case is O((N log N)^2)).

I think that worst case should be a a sequence containing largest vales in odd positions. However, here some gap sequences are introduced with Θ(N^3/2) complexity.

I am trying to figure out what is the actual worst case for Shell sort. So far, according to aforementioned paper, the worst case is O((N log N)^2)) not Θ(N^3/2). In addition, here suggested a worst scenario analysis, that apparently, is not Θ(N^3/2).

Here, a time complexity analysis is done on a certain algorithm with O(N^2) as its worst case.

But, I am completely lost. What is the worst case for Shell sort?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
David
  • 1,343
  • 3
  • 12
  • 13
  • Related: https://stackoverflow.com/questions/12767588/time-complexity-for-shell-sort (although the answer here does not provide links and does not cover all the questions asked above). – user2864740 Oct 12 '17 at 16:56

1 Answers1

1

It looks like there isn't just one "Shellsort", but a family of sorting functions parameterized by what is called a gap sequence. Shellsort works by h-sorting a list multiple times for decreasing values of h. The sequences of h's used determines how Shellsort performs. Some sequences give O(N^3/2), some give O(N^2), others give O(N log^2 N), etc.

Odds are each reference you see is using a different gap sequence to derive their asymptotic bounds.

Edit: consider the worst possible gap sequence (without repeats) n, n-1, n-2, ..., 1. To get the runtime:

h    sublists sublist size    comparisons
n    n        1 (n)           0
n-1  n-1      1 (n-2), 2 (1)  1
n-2  n-2      1 (n-4), 2 (2)  2
...
n/2  n/2      2 (n/2)         2n
...
n/3  n/3      3 (n/3)         3n
...
n/4  n/4      4 (n/4)         4n
...
n/n           n (1)           n^2

So the answer is going to be something like n(1+2+...+n) = n^2(n+1)/2, or O(n^3). This is what I'm thinking the maximum possible complexity would be for gap strictly decreasing gap sequences (gap sequences that aren't strictly decreasing are not interesting since those could be arbitrarily bad).

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • So what is the worst 'worst case'? is it O(N^3/2)? – David Oct 12 '17 at 17:31
  • @David What's your gap sequence? Shellsort is not well-defined until you select one. I suppose the worst possible case would be choosing the sequence n, n-1, n-2, ..., 1, which is strictly worse than bubble sort. I could try to work out the complexity in this case. – Patrick87 Oct 12 '17 at 17:58
  • (1+2+...+n) is because of sublists and their size, right? if this is the case, What the 'h' is? – David Oct 15 '17 at 20:32
  • `h` is the *gap* in the *gap sequence*. Shellsort works by `h`-sorting a list over and over again, eventually 1-sorting it to ensure correctness. For `h = n`, we mean we make sure every that the subsequence whose indices in the list to be sorted are congruent modulo `n` is sorted. For `h = 3`, we make sure that the subsequences whose indices in the list to be sorted are (1, 4, 7, ...), (2, 5, 8, ...) and (3, 6, 9, ...) are sorted. All indices are congruent modulo 1, so finishing with `h = 1` ensures the whole list is sorted. – Patrick87 Oct 16 '17 at 12:33