The Shell Sort
The Shell Sort is a variation of the Insertion
Sort.
The run time is O(n log n) (with probability
near 100%).
The worst case has run time O(n2), but it is
exceedingly unlikely.
08/12/25 Sorting 1
The Shell Sort Algorithm
Start with a “gap” equal to half the size of the
list.
Perform Insertion Sorts on the interwoven
sublists (k = gap).
{a0, ak, a2k, ...}
{a1, ak + 1, a2k + 1, ...}
{a2, ak + 2, a2k + 2, ...},
:
{a
k - 1, a2k - 1, a3k - 1, ...}.
08/12/25 Sorting 2
Example: The Shell Sort
Begin with a list of size n = 9.
Initialize gap = 4
Pass #1, first sublist.
30 60 80 20 90 50 10 70 40
0 4 8
30 60 80 20 40
90 50 10 70 90
40
0 4 8
08/12/25 Sorting 3
Example: The Shell Sort
Pass #1, 2nd sublist.
30 60 80 20 40 50 10 70 90
1 5
30 50
60 80 20 40 50
60 10 70 90
1 5
08/12/25 Sorting 4
Example: The Shell Sort
Pass #1, 3rd sublist.
30 50
60 80 20 40 50
60 10 70 90
2 6
30 50 10
80 20 40 60 10
80 70 90
2 6
08/12/25 Sorting 5
Example: The Shell Sort
Pass #1, 4th sublist.
30 50 10
80 20 40 60 10
80 70 90
3 7
30 50 10 20 40 60 80 70 90
3 7
08/12/25 Sorting 6
Example: The Shell Sort
Divide gap by 2 and repeat the above
procedure.
Quit when gap = 0.
08/12/25 Sorting 7
Example: The Shell Sort
Halve the gap: gap = 4 / 2 = 2.
Pass #2, 1st sublist
30 50 10 20 40 60 80 70 90
0 2 4 6 8
30 50 10
10 30 20 40 60 80 70 90
0 2 4 6 8
08/12/25 Sorting 8
Example: The Shell Sort
Halve the gap: gap = 4 / 2 = 2.
Pass #2, 2nd sublist
10 50 30 20 40 60 80 70 90
1 3 5 7
10 20
50 30 20
50 40 60 80 70 90
1 3 5 7
08/12/25 Sorting 9
Example: The Shell Sort
Halve the gap: gap = 2 / 2 = 1.
10 20 30 50 40 60 80 70 90
0 1 2 3 4 5 6 7 8
10 20 30 40
50 50
40 60 70
80 80
70 90
0 1 2 3 4 5 6 7 8
08/12/25 Sorting 10