Navigation

How to perform a Shell Sort

Shell Sort is really just an extension of Insertion Sort, with two
observations in mind:

Insertion Sort is efficient if the input is "almost sorted".

Insertion Sort is inefficient, on average, because it moves values just
one position at a time.

Shell Sort is similar to Insertion Sort, but it works by taking "bigger
steps" as it rearranges values, gradually decreasing the step size down
towards one. In the end, Shell Sort performs a regular Insertion Sort but by
then, the array of data is guaranteed to be "almost sorted".

Consider a small value that is initially stored in the wrong end of the
array. Using Insertion Sort, it will take roughly N comparisons and
exchanges to move this value all the way to the other end of the array. With
Shell Sort, we'll first move values using giant step sizes, so a small value
will move a long way towards it's final position, with just a few
comparisons and exchanges.