When analyzing insertion sort, computer scientists noticed that the distance an item is from its sorted index had a direct impact on how many swaps needed to be performed. This is where shell sort came along and attempted to address this issue.

### Assumptions

- Knowledge of Big-O notation
- Familiarity with insertion sort

# What is Shell Sort?

Shell Sort is an in place comparison sort, often viewed as a variation of the insertion sort algorithm. The algorithm sorts pairs that are far apart from each other, shrinking the gap each iteration until the array is sorted. This makes later moves comparatively less expensive to perform.

> Shell sort was invented by Donald Shell in 1959.

The overview of how to perform shell sort is as follows:

- Define a "gap" as a size relative to a fraction of the array.
- Sort the fractions using insertion sort.
- Repeat while the list remains unsorted.

```
/* pseudo-code */
function shellSort( int[] arr ) {
int gap = 0;
// (*)
while ( gap < arr.length / 3 ) {
gap = gap * 3 + 1;
}
while ( gap > 0 ) {
// Perform insertion sort on the gap.
for ( int i = gap; i < arr.length; i++ ) {
int j = i;
int tmp = arr[i];
// While there exists an inversion on the sub array..
while ( j >= gap && arr[j - gap] > tmp ) {
// .. Shift the elements down..
arr[j] = arr[j - gap];
j = j - gap;
}
// .. And set the correct element.
arr[j] = tmp;
}
// (*)
gap = ( gap - 1 ) / 3;
}
}
```

> *This version of Shell sort uses Knuth's formula, which helps the algorithm achieve good performance based on a derived formula for an efficient order. There are many more competing methods that have been devised for the most efficient gap, though.

## Analysis of Shell Sort

Best | Worst | Memory |
---|---|---|

O(n log n) | O(n<sup>2</sup>) | O(1) |

In the best case scenario where an array is already sorted, no shifts would need to be performed, and the gap would converge logarithmically to 0. This results in **O(n)** time to process the array and **O(log n)** time to converge the gap to 0, resulting in **O(n log n)** time in the best case.

Worst case scenario, it performs just as insertion sort would, meaning that it could potentially take up to **O(n<sup>2</sup>)** time to complete the sort.

## When to use Shell Sort

Because shell sort is able to move items that are far away from their needed place with efficiency, it is sometimes preferred on nearly sorted arrays compared to insertion sort. Being that it is in place as well makes it suitable for systems where memory constraints are a concern.

## Further Resources

- <a href="https://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X/ref=as_li_ss_tl?_encoding=UTF8&pd_rd_i=032157351X&pd_rd_r=37c135e7-c5c4-4ed1-94ab-c2aaec60d692&pd_rd_w=PEuac&pd_rd_wg=xSJkC&pf_rd_p=1c11b7ff-9ffb-4ba6-8036-be1b0afa79bb&pf_rd_r=H3XK6W265MFMNYZ7PV3Z&psc=1&refRID=H3XK6W265MFMNYZ7PV3Z&linkCode=ll1&tag=devmaking-20&linkId=9f9600161b287607f80038c5fd85b020&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Sedgwick)</a> An excellent resource and reference for learning algorithms in computer science.
- <a href="https://www.amazon.com/Algorithms-Sanjoy-Dasgupta-ebook-dp-B006Z0QR3I/dp/B006Z0QR3I/ref=as_li_ss_tl?_encoding=UTF8&me=&qid=1566246918&linkCode=ll1&tag=devmaking-20&linkId=26b190aacb9c02d1a0815d4066f9d80e&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Dasgupta)</a> Concise and clear overview on various types and designs of algorithms in a digestible format.
- <a href="https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=devmaking-20&linkId=1dd5be6066b180c12926dc03e2659215&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Algorithms (CLRS)</a> An in depth and comprehensively detailed explanation of algorithms and how to design them.
- <a href="https://www.amazon.com/Introduction-Graph-Theory-Dover-Mathematics/dp/0486678709/ref=as_li_ss_tl?keywords=graph+theory&qid=1566248143&s=books&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExUVdSUTU4TjVPRzdIJmVuY3J5cHRlZElkPUEwNzM2MDIyMkIxUUdNVENLMVJFUiZlbmNyeXB0ZWRBZElkPUEwNzc2NTcyNDBEMlFGWDJRRU5XJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==&linkCode=ll1&tag=devmaking-20&linkId=3a3d02f5e5b338587f4e4e4be75fe2f2&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Graph Theory</a> An accessible, mathematic-centered approach to graph theory for new programmers and mathematicians.