In this tutorial, you will learn how shell sort works. Also, you will find working examples of shell sort in Python.

Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces the interval between the elements to be sorted. It is a generalized version of the insertion sort.

In shell sort, elements at a specific interval are sorted. The interval between the elements is gradually decreased based on the sequence used. The performance of the shell sort depends on the type of sequence used for a given input array.Â Some of the optimal sequences used are:

• Shellâ€™s original sequence:Â `N/2 , N/4 , â€¦, 1`
• Knuthâ€™s increments:Â `1, 4, 13, â€¦, (3k â€“ 1) / 2`
• Sedgewickâ€™s increments:Â `1, 8, 23, 77, 281, 1073, 4193, 16577...4j+1+ 3Â·2j+ 1`
• Hibbardâ€™s increments:Â `1, 3, 7, 15, 31, 63, 127, 255, 511â€¦`
• Papernov & Stasevich increment:Â `1, 3, 5, 9, 17, 33, 65,...`
• Pratt:Â `1, 2, 3, 4, 6, 9, 8, 12, 18, 27, 16, 24, 36, 54, 81....`

#### How Shell Sort Works?

1. Suppose, we need to sort the following array.
2. We are using the shellâ€™s original sequenceÂ `(N/2, N/4, ...1`) as intervals in our algorithm.

In the first loop, if the array size isÂ `N = 8` then, the elements lying at the intervalÂ Â `N/2 = 4`Â are compared and swapped if they are not in order.

1. The 0th element is compared with theÂ 4thÂ element.
2. If the 0th element is greater than theÂ 4thÂ one then, theÂ 4thÂ element is first stored inÂ `temp` a variable and theÂ `0th`Â element (ie. greater element) is stored in theÂ `4th`Â position and the element stored inÂ `temp`Â is stored in theÂ `0th`Â position.

This process goes on for all the remaining elements.

3. In the second loop, an interval ofÂ `N/4 = 8/4 = 2`Â is taken and again the elements lying at these intervals are sorted.

You might get confused at this point.

The elements atÂ 4thÂ andÂ `2nd`Â position are compared. The elements atÂ 2ndÂ andÂ `0th`Â position are also compared. All the elements in the array lying at the current interval are compared.

4. The same process goes on for the remaining elements.
5. Finally, when the interval isÂ `N/8 = 8/8 =1`Â then the array elements lying at the interval of 1 are sorted. The array is now completely sorted.

#### Complexity

Shell sort is an unstable sorting algorithm because this algorithm does not examine the elements lying in between the intervals.

#### Time Complexity

• Worst Case Complexity: less than or equal toÂ `O(n2)`
Worst-case complexity for shell sort is always less than or equal to `O(n2)`.

According to the Poonen Theorem, worst case complexity for shell sort is `Î˜(Nlog N)2/(log log N)2)`Â orÂ `Î˜(Nlog N)2/log log N)`Â orÂ `Î˜(N(log N)2)`Â or something in between.

• Best Case Complexity:Â `O(n*log n)`
When the array is already sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
• Average Case Complexity:Â `O(n*log n)`
It is aroundÂ `O(n1.25)`.

The complexity depends on the interval chosen. The above complexities differ for different increment sequences chosen. Best increment sequence is unknown.

#### Space Complexity:

The space complexity for shell sort isÂ `O(1)`.

#### Shell Sort Applications

Shell sort is used when:

• calling a stack is overhead. uClibc library uses this sort.
• recursion exceeds a limit. bzip2 compressor uses it.
• Insertion sort does not perform well when the close elements are far apart. Shell sort helps in reducing the distance between the close elements. Thus, there will be less number of swappings to be performed.