|
| 1 | +# Shellsort |
| 2 | + |
| 3 | +Shellsort, also known as Shell sort or Shell's method, |
| 4 | +is an in-place comparison sort. It can be seen as either a |
| 5 | +generalization of sorting by exchange (bubble sort) or sorting |
| 6 | +by insertion (insertion sort). The method starts by sorting |
| 7 | +pairs of elements far apart from each other, then progressively |
| 8 | +reducing the gap between elements to be compared. Starting |
| 9 | +with far apart elements, it can move some out-of-place |
| 10 | +elements into position faster than a simple nearest neighbor |
| 11 | +exchange |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## How Shell Sort Works |
| 16 | + |
| 17 | +For our example and ease of understanding, we take the interval |
| 18 | +of `4`. Make a virtual sub-list of all values located at the |
| 19 | +interval of 4 positions. Here these values are |
| 20 | +`{35, 14}`, `{33, 19}`, `{42, 27}` and `{10, 44}` |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +We compare values in each sub-list and swap them (if necessary) |
| 25 | +in the original array. After this step, the new array should |
| 26 | +look like this |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Then, we take interval of 2 and this gap generates two sub-lists |
| 31 | +- `{14, 27, 35, 42}`, `{19, 10, 33, 44}` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +We compare and swap the values, if required, in the original array. |
| 36 | +After this step, the array should look like this |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +Finally, we sort the rest of the array using interval of value 1. |
| 41 | +Shell sort uses insertion sort to sort the array. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## References |
| 46 | + |
| 47 | +* [Tutorials Point](https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm) |
| 48 | +* [Wikipedia](https://en.wikipedia.org/wiki/Shellsort) |
0 commit comments