Skip to content

Shell sort

ordering

Level 2 of 3

Also called Diminishing increment sort.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from shell sort's own run on the numbers shown.

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.

new numbers

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.

new numbers

question 3

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.

new numbers