Model answer
For [8,7,6,5,4,3,2,1], gap 4 gives [4,3,2,1,8,7,6,5], gap 2 gives [2,1,4,3,6,5,8,7], and gap 1 finishes [1,2,3,4,5,6,7,8]. Each pass is insertion sort on gap-separated subsequences.
Use the key terms, then explain the reasoning.
Shell sort performs insertion sorts over decreasing gaps so distant inversions are removed before the final gap-one pass.
Shell sort generalises insertion sort by first sorting elements far apart. Large gaps move badly displaced values quickly; decreasing gaps reduce remaining local disorder before the mandatory gap-one insertion pass.
Its exact complexity depends on the chosen gap sequence. The simple halving sequence is easy to teach, while sequences such as Knuth or Ciura usually perform better.
| Operation | Best | Average | Worst | Extra space |
|---|---|---|---|---|
| Sort | ≈O(n^1.5) | ≈O(n^1.5) | O(n²) | O(1) |
More advanced gap sequences improve performance, but the core gapped insertion idea is unchanged.
Open a prompt when you are ready to check your answer.