Skip to content

Shell sort

ordering

Level 2 of 3

Also called Diminishing increment sort.

test yourself

Six questions, four to pick from and two for paper, each answer hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Shell sort works on 6, 9, 1, 8, 3, 5, 2. What is the row after its first move?

6918352
the answer, and why

the answer is C

7 numbers take the gaps 4 and 1. The gap 4 pass first picks up the 3 at place 5. The 6, 4 places left, is bigger, so they trade.

if you chose another

  • That is insertion sort's first move, trading neighbours.
  • That trades numbers 3 places apart, the 9 and the 3.
  • That is after the whole gap 4 pass, which also trades the 9 and the 5.

question 2

How many moves does Shell sort make, from start to finish, on 6, 7, 8, 9, 2, 3, 4, 5?

67892345
the answer, and why

the answer is B

The gap 4 pass trades 6 with 2, 7 with 3, 8 with 4 and 9 with 5. After those 4 moves the row is in order.

the gap 4 pass: 4 comparisons, 4 moves67892345startthe gap 1 pass: 7 comparisons, no move23456789after gap 423456789after gap 1a pair compareda pair tradedmoved by the pass beforethe row at the end
The gap 4 pass trades all four pairs; the gap 1 pass moves nothing.

if you chose another

  • That is insertion sort's count, one move per wrong pair.
  • That counts the comparisons, 4 + 7.
  • That counts the numbers that change place. Each move trades two.

question 3

Which is true of Shell sort's moves on any row?

the answer, and why

the answer is A

Each move leaves at least one wrong pair fewer, so the moves are at most the row's wrong pairs, insertion sort's count.

this tests where the formula comes from, on the learn page

if you chose another

  • On a row in order both make none.
  • That is insertion sort. On 9, 7, 3, 5, 2, 4 Shell sort's first move mends 7 wrong pairs.
  • On 7, 6, 5, 4, 3, 2, 1 it makes 9.

question 4

Shell sort works on each of these orders of 1 to 7. On which does it make the most comparisons?

the answer, and why

the answer is B

Its gap 4 pass makes 3 comparisons and leaves 12 of its 14 wrong pairs. So the gap 1 pass makes 12 moves, and 16 comparisons, one per move and one for each of 4 numbers stopping short of the front. 19 in all.

the gap 4 pass: 3 comparisons, 2 moves6417532startthe gap 1 pass: 16 comparisons, 12 moves5317642after gap 41234567after gap 1a pair compareda pair tradedmoved by the pass beforethe row at the end
6, 4, 1, 7, 5, 3, 2 pass by pass, 3 comparisons and then 16.

if you chose another

  • 13 comparisons. The gap 4 pass leaves 6 of its 21 wrong pairs.
  • 11 comparisons. The gap 4 pass leaves 3 of its 12 wrong pairs.
  • 11 comparisons. The gap 4 pass moves nothing, but only 3 pairs are wrong.

pen and paper

question 5about 15 minutes

Show that Shell sort makes (n−1)+(n−4)+(n−13)+…(n - 1) + (n - 4) + (n - 13) + \dots comparisons on nn numbers already in order, that no row makes fewer, and that this count is O(n log⁡2 n)O(n \,\log_2\, n).

  1. Count one pass on a row in order, and add up the passes.
  2. Say why no row makes fewer.
  3. Show that at most log⁡2 n+1\log_2\, n + 1 gaps lie below nn, and find a ceiling of a fixed multiple of n log⁡2 nn \,\log_2\, n.
a hint

Each gap more than doubles, so compare the gaps with 1, 2, 4, 8 and on.

a worked answer

Each pass compares its n−hn - h numbers once, and nothing moves; on any row each is compared at least once. Gap number kk is at least 2k−12^{k - 1}, so at most log⁡2 n+1\log_2\, n + 1 gaps lie below nn, and the count is at most 2n log⁡2 n2n \,\log_2\, n. For 8 numbers, 11, at most 48.

Why does a row already in order take (n−1)+(n−4)+(n−13)+…(n - 1) + (n - 4) + (n - 13) + \dots comparisons, the fewest any row takes, and what ceiling holds?
  1. the move: count one pass on a row in order

    the line:
    on a row in order,the pass with gap hpicks up places h+1 to nand compares n−h times\begin{gathered} \text{on a row in order,} \\ \text{the pass with gap } h \\ \text{picks up places } h + 1 \text{ to } n \\ \text{and compares } n - h \text{ times} \end{gathered}
    the gap 4 pass: 4 comparisons, no move712182331424956startthe gap 1 pass: 7 comparisons, no move712182331424956after gap 4712182331424956after gap 1a pair comparedthe row at the end
    8 numbers in order. 4 comparisons with the gap 4, then 7 with the gap 1, and no move.

    why it is allowed: Each number picked up meets a number no bigger hh places to its left, so it stays put after one comparison. For 8 numbers and the gap 4, places 5 to 8, 4 comparisons.

  2. the move: list the gaps

    the line:
    the gaps 1,4,13,40,…,each three times the onebefore and 1 more;from 2 numbers on, one passfor each gap below n,the largest first and 1 last\begin{gathered} \text{the gaps } 1, 4, 13, 40, \dots \text{,} \\ \text{each three times the one} \\ \text{before and 1 more;} \\ \text{from 2 numbers on, one pass} \\ \text{for each gap below } n \text{,} \\ \text{the largest first and 1 last} \end{gathered}

    why it is allowed: 3 times 1, and 1, makes 4; 3 times 4, and 1, makes 13. So 8 numbers take the gaps 4 and 1.

  3. the move: add up the passes

    the line:
    B=(n−1)+(n−4)+(n−13)+…one term for eachgap below n\begin{gathered} B = (n - 1) + (n - 4) \\ {} + (n - 13) + \dots \\ \text{one term for each} \\ \text{gap below } n \end{gathered}

    why it is allowed: BB is the comparisons on a row in order, lines 1 and 2 added up. For 8 numbers, 7 + 4, which makes 11.

  4. the move: no row costs fewer

    the line:
    no row of n numberscosts fewer than B\begin{gathered} \text{no row of } n \text{ numbers} \\ \text{costs fewer than } B \end{gathered}

    why it is allowed: On any row each pass picks up the same places, each with a number hh places to its left, so each is compared at least once.

  5. the move: so this row is a cheapest one

    the line:
    a row already in orderis a cheapest row\begin{gathered} \text{a row already in order} \\ \text{is a cheapest row} \end{gathered}

    why it is allowed: By lines 3 and 4. Others tie, as 2, 1, 3, 4, 5, 6, 7, 8 also costs 11.

  6. the move: a ceiling for each term

    the line:
    each term of Bis less than n\begin{gathered} \text{each term of } B \\ \text{is less than } n \end{gathered}

    why it is allowed: Each is nn less a gap of at least 1. For 8 numbers, 7 and 4.

  7. the move: add up the ceilings

    the line:
    B≤G×nB \le G \times n

    why it is allowed: GG is how many gaps lie below nn, so by lines 3 and 6, BB is GG terms, each less than nn. For 8 numbers, 11 is at most 2 times 8.

  8. the move: each gap more than doubles

    the line:
    each gap is more thantwice the one before\begin{gathered} \text{each gap is more than} \\ \text{twice the one before} \end{gathered}
    numberlog× 2+ 121831641042133.70
    The gaps 1, 4 and 13 on a line of powers of two, where each step doubles. Each gap sits more than one step right of the one before.

    why it is allowed: By line 2, three times a gap, and 1, is more than twice it.

  9. the move: compare the gaps with the powers of 2

    the line:
    gap number k is at least 2k−1\text{gap number } k \text{ is at least } 2^{k - 1}
    kkgap number kk2k−12^{k - 1}
    111
    242
    3134
    4408
    512116
    Each gap beside the power of 2 with its number, equal at 1, then ever further above.

    why it is allowed: Gap number 1 is 1, 2 to the power 0. Each power of 2 is twice the one before, and each gap more than twice, by line 8. So when one gap is at least its power of 2, the next is too, and so on for ever.

  10. the move: name the largest gap below n

    the line:
    from 2 numbers on,the largest gap below nis gap number G\begin{gathered} \text{from 2 numbers on,} \\ \text{the largest gap below } n \\ \text{is gap number } G \end{gathered}

    why it is allowed: From 2 numbers on the gap 1 is below nn, and the gaps grow, by line 8. So the GG gaps below nn are gap numbers 1 to GG.

  11. the move: use line 9 on the largest gap

    the line:
    gap number G≥2G−1\text{gap number } G \ge 2^{G - 1}

    why it is allowed: Line 9 with GG for kk. For 8 numbers, 4 is at least 2.

  12. the move: the largest gap is at most n

    the line:
    gap number G≤n\text{gap number } G \le n

    why it is allowed: By line 10. For 8 numbers, 4 is at most 8.

  13. the move: put the two together

    the line:
    2G−1≤n2^{G - 1} \le n

    why it is allowed: By lines 11 and 12.

  14. the move: read it as a log

    the line:
    G−1≤log⁡2 nG - 1 \le \log_2\, n

    why it is allowed: Take the log of both sides of line 13. A bigger number has a bigger log, as the page on halving and logarithms shows, and the log of 2 to a power is that power. For 8 numbers, 1 is at most 3.

  15. the move: add 1 to both sides

    the line:
    G≤log⁡2 n+1G \le \log_2\, n + 1

    why it is allowed: Line 14 plus 1.

  16. the move: multiply both sides by n

    the line:
    G×n≤n(log⁡2 n+1)G \times n \le n(\log_2\, n + 1)

    why it is allowed: Line 15 times nn, above zero. For 8 numbers, 16 is at most 32.

  17. the move: put the count of gaps in

    the line:
    B≤n(log⁡2 n+1)B \le n(\log_2\, n + 1)

    why it is allowed: By lines 7 and 16. For 8 numbers, 11 is at most 32.

  18. the move: a fact for a simpler ceiling

    the line:
    1≤log⁡2 n for n≥21 \le \log_2\, n \text{ for } n \ge 2

    why it is allowed: From 2 numbers on nn is at least 2, whose log is 1, and a bigger number has a bigger log.

  19. the move: add the log to both sides

    the line:
     log⁡2 n+1≤log⁡2 n+log⁡2 nfor n≥2\begin{gathered} \,\log_2\, n + 1 \le \log_2\, n + \log_2\, n \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Line 18 plus log⁡2 n\log_2\, n.

  20. the move: add like to like

    the line:
    log⁡2 n+1≤2 log⁡2 n for n≥2\log_2\, n + 1 \le 2 \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: Two lots of log⁡2 n\log_2\, n.

  21. the move: multiply both sides by n

    the line:
    n(log⁡2 n+1)≤2n log⁡2 nfor n≥2\begin{gathered} n(\log_2\, n + 1) \le 2n \,\log_2\, n \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Line 20 times nn, above zero.

  22. the move: a simpler ceiling

    the line:
    B≤2n log⁡2 n for n≥2B \le 2n \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: By lines 17 and 21. For 8 numbers, 11 is at most 48.

  23. the move: read it as a bound

    the line:
    B∈O(n log⁡2 n)B \in O(n \,\log_2\, n)

    why it is allowed: By line 22, under twice n log⁡2 nn \,\log_2\, n, a fixed multiple.

try it with a small nn: at n=8n = 8, line 3 gives B=11B = 11, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • one pass compares n - h times, one term per gap below n
  • on any row each number picked up is compared at least once
  • at most log⁡2 n+1\log_2\, n + 1 gaps below nn, with a reason
  • a ceiling such as 2n log⁡2 n2n \,\log_2\, n, with its multiple named

question 6about 20 minutes

A move of Shell sort trades a number with a smaller one hh places to its right. Show that each move leaves fewer wrong pairs, so the moves are at most the wrong pairs at the start.

  1. Say what the move does to its own pair.
  2. Take a number between the two, and check every way its size can compare with theirs.
  3. Say why no other pair changes, and finish the argument.
  4. Check it on 9, 4, 8, 1, 6, 3.
a hint

Split it by size, smaller than both, between, bigger than both, or equal to one.

a worked answer

The traded pair is now in order. A number between them keeps one wrong pair with them if smaller or bigger than both, loses two if between them in size, and loses one if equal to one. Other pairs keep their order. So each move leaves at least one fewer, and the sort ends with none. 9, 4, 8, 1, 6, 3 has 11 wrong pairs and takes 9 moves, 2 of them with the gap 4.

this tests where the formula comes from, on the learn page

what a full answer includes
  • the traded pair is now in order
  • each way a number between them can compare in size, with its wrong pairs
  • other pairs keep their order
  • the sort ends in order, so moves are at most the wrong pairs
  • 11 wrong pairs and 9 moves