Shell sort is an unstable quadratic sorting algorithm, which can be seen as a generalization of insertion sort. Althrought is has an O(n^2) asymptotic complexity, it is the most efficient algorithm of this class.

Description

An ordinary insertion sort maintains a list of already sorted elements. Than it picks the element next to the list and places it at the correct position within the list. By iteratively repeating this procedure (starting with a list of one element) the array gets sorted in n steps.

Shell sort operates analogously. The main difference is, that Shell sort uses so called diminishing increment. It means that in every step only elements at some distance are compared (for example the first with the fifth, second with the sixth...). This approach ensures that elements with high and low value are moved to the appropriate side of the array very quickly. In every iteration the gap between the compared elements is reduced. In the iteration step, the gap is set to one – the algorithm degrades to an ordinary insertion sort, which terminates very quickly, because now the array contains only few misplaced elements.

Optimal gap

The fundamental problem of Shell sort is to determine the optimal gap between compared elements. In the original algorithm Donald Shell proposed an initial gap of size n/2 (n is the size of the array), divided by 2 in each step. Thich approach has one big disadvantage – elements at odd and even places are mutually compared only in the last step.

Other implementations used gap size 2^{k} - 1 (Hibbard) with the worst case complexity O(n^{3/2}), or 9 \\cdot 4^{i} - 9 \\cdot 2^{i} (Sedgewick) with complexity O(n^{4/3}). The best performance is offered by a sequence by Marcin Ciura - 1, 4, 10, 23, 57, 132, 301, 701, 1750 every next gap size is generated by multiplying the previous size by 2.2.

Visualization


Code

01./**
02. * Shell sort - sort with diminishing increment (descending)
03. * @param array to be sorted
04. * @return sorted array
05. */
06.public static int[] shellSort(int[] array) {
07.    int gap = array.length / 2;
08.    while (gap > 0) {
09.        for (int i = 0; i < array.length - gap; i++) { //modified insertion sort
10.            int j = i + gap;
11.            int tmp = array[j];
12.            while (j >= gap && tmp > array[j - gap]) {
13.                array[j] = array[j - gap];
14.                j -= gap;
15.            }
16.            array[j] = tmp;
17.        }
18.        if (gap == 2) { //change the gap size
19.            gap = 1;
20.        } else {
21.            gap /= 2.2;
22.        }
23.    }
24.    return array;
25.}
01./**
02. * Shell sort - sort with diminishing increment (descending)
03. * @param array to be sorted
04. * @param size array size
05. * @return sorted array
06. */
07.int * shellSort(int * array, int size) {
08.    int gap = size / 2;
09.    while (gap > 0) {
10.        for (int i = 0; i < size - gap; i++) { //modified insertion sort
11.            int j = i + gap;
12.            int tmp = array[j];
13.            while (j >= gap && tmp > array[j - gap]) {
14.                array[j] = array[j - gap];
15.                j -= gap;
16.            }
17.            array[j] = tmp;
18.        }
19.        if (gap == 2) { //change the gap size
20.            gap = 1;
21.        } else {
22.            gap /= 2.2;
23.        }
24.    }
25.    return array;
26.}
01./**
02. * Shell sort - sort with diminishing increment (descending)
03. * @param array to be sorted
04. * @return sorted array
05. */
06. public static int[] ShellSort(int[] array)
07. {
08.     int gap = array.Length / 2;
09.     while (gap > 0)
10.     {
11.         for (int i = 0; i < array.Length - gap; i++) //modified insertion sort
12.         {
13.             int j = i + gap;
14.             int tmp = array[j];
15.             while (j >= gap && tmp > array[j - gap])
16.             {
17.                 array[j] = array[j - gap];
18.                 j -= gap;
19.             }
20.             array[j] = tmp;
21.         }
22.         if (gap == 2) //change the gap size
23.         {
24.             gap = 1;
25.         }
26.         else
27.         {
28.             gap = (int)(gap / 2.2);
29.         }
30.     }
31.     return array;
32. }







       
 

Place for your banner

Here is the position ready for our customer's banners.