taylorialcom/ Sorting

Shell Sort

Insertion Sort has good best case performance \( O(n) \) but mediocre worst case performance \( O(n^2) \). Shell Sort attempts to optimize Insertion Sort to improve the worst case time complexity.

The worst case performance of Insertion Sort suffered when we had long chains of swaps when needing to move a small item for the yet-to-be-sorted partition to the beginning of the sorted partition. Shell Sort seeks to minimize long chains of swaps.

Consider:

238561

Here the first three are already sorted, so no swaps are needed, but then

238561
235861
235681
123568

Suppose we started by swapping 8 and 1 first, and then execute as we normally would.

231568
123568

Shell Sort uses this idea by making multiple passes through the array, starting with a large gap and progressively reducing it. In 1959, Dr. Donald Shell authored a two page paper introducing Shell Sort. In it he suggested using a gap of (( \lfloor \frac{n}{2^k} \rfloor \) for the \( k^{th} \) iteration.

Shell Sort

Worst case performance

Worst Case Shell Sort

This requires fewer swaps in the worst case.

Over the past 60+ years computer scientists have published papers describing the merits of different criteria for selecting the gap sequence. In 1986, Dr. Robert Sedgewick published a paper demonstrating that the worst case time complexity could be as low as \( O(n^{1.33}) \) with an appropriately chosen gap sequence.