Shell sort

Shell sort (or Shellsort) is one of the oldest sorting algorithms. It was invented in 1959 by Donald L. Shell [Sh]. It is fast, easy to understand and easy to implement. However, its complexity analysis is a little more sophisticated. It is easy to develop an intuitive sense of how this algorithm works, but often difficult to analyze its execution time.

Shell sort is sometimes called "Shell-Metzner sort" after Marlene Metzner who wrote a very early implementation of Shell sort in FORTRAN. It was first called Shell-Metzner in an article in Creative Computing in 1976, but Marlene Metzner has said that her name should never have been attached to it.

Contents

Basic concept

Shell sort is an extension of insertion sort, with two observations in mind:

  1. Insertion sort is efficient if the input is almost sorted.
  2. Insertion sort is inefficient, on average, because it moves values just one position at a time.

Shell sort is similar to insertion sort, but it works by taking bigger steps as it rearranges values, gradually decreasing the step size down towards one. In the end, Shell sort performs a regular insertion sort, but by then, the array of data is guaranteed to be almost sorted.

Consider a small value that is initially stored in the wrong end of the array. Using insertion sort, it will take roughly n comparisons and exchanges to move this value all the way to the other end of the array. With Shell sort, we'll first move values using giant step sizes, so a small value will move a long way towards its final position, with just a few comparisons and exchanges.

The idea of Shell sort can be illustrated in the following way:

  • arrange the data sequence in a two-dimensional array
  • sort the columns of the array

The effect is that the data sequence is partially sorted. The process above is repeated, but each time with a narrower array, i.e. with a smaller number of columns. In the last step, the array consists of only one column. In each step, the sortedness of the sequence is increased, until in the last step it is completely sorted. However, the number of sorting operations necessary in each step is limited, due to the presortedness of the sequence obtained in the preceding steps.

Example

Let

3 7 9 0 5 1 6 8 4 2 0 6 1 5 7 3 4 9 8 2

be the data sequence to be sorted. First, it is arranged in an array with 7 columns (left), then the columns are sorted individually (right), with the largest element of each column at the bottom:

3 7 9 0 5 1 6          3 3 2 0 5 1 5
8 4 2 0 6 1 5    ->    7 4 4 0 6 1 6
7 3 4 9 8 2            8 7 9 9 8 2

Data elements 8 and 9 have now already come to the end of the sequence, but a small element (2) is also still there. In the next step, the sequence is arranged in 3 columns, which are again sorted:

3 3 2          0 0 1
0 5 1          1 2 2
5 7 4          3 3 4
4 0 6    ->    4 5 6
1 6 8          5 6 8
7 9 9          7 7 9
8 2            8 9

Now the sequence is almost completely sorted. When arranging it in one column in the last step, it is only a 6, an 8 and a 9 that have to move a little bit to their correct position.

In reality, the data sequence is not arranged in a two-dimensional array, but held in a one-dimensional array that is indexed appropriately. For instance, data elements at positions 0, 5, 10, 15, etc. would form the first column of an array with 5 columns. The "columns" obtained by indexing in this way are sorted with Insertion sort, since this method has a good performance with presorted sequences.

Analysis

The correctness of the algorithm follows from the fact that in the last step (with h = 1) an ordinary insertion sort is performed on the whole array. But since data are presorted by the preceding steps (h = 3, 7, 31, ...), only a few insertion sort steps are sufficient. The exact number depends on the sequence of h values (denoted as h-sequence). The h-sequence above is just one of several possible.

(Pratt)With the h-sequence 1, 2, 3, 4, 6, 8, 9, 12, 16, ..., 2p3q, ... Shellsort needs O(n·log(n)2) steps for sorting a sequence of length n.

(Hibbard) With the h-sequence 1, 3, 7, 15, 31, 63, 127, ..., 2k - 1, ... Shellsort needs O(n3/2) steps for sorting a sequence of length n.

(Knuth) With the h-sequence 1, 4, 13, 40, 121, ..., 3hs-1 + 1 = (3s - 1)/2, ... Shellsort needs O(n3/2) steps for sorting a sequence of length n.

(Sedgewick) With the h-sequence 1, 5, 19, 41, 109, 209, ... (described below), Shellsort needs O(n4/3) steps for sorting a sequence of length n.

<math>
 h_s=
 \left\{
  \begin{matrix}
   9 \cdot 2^s - 9 \cdot 2^{s/2} + 1 &     \mbox{if }s\mbox{ is even}  \\
   8 \cdot 2^s - 6 \cdot 2^{(s+1)/2} + 1 & \mbox{if }s\mbox{ is odd} \\
  \end{matrix}
 \right.
<math>

(Insertion sort) The worst case of Shell sort is the basic insertion sort (using a single h-step of 1), which requires O(n²) comparisons and exchanges.

An easily computable h-sequence for Shell sort, and a really poor one, is the Fibonacci Sequence (1, 2, 3, 5, 8, 13, 21, ... ), which increases step size in a natural progression.

The best known sequence of increments in terms of the average number of comparisons made is 1, 4, 10, 23, 57, 132, 301, 701 (further increments unknown) [1] (http://www-zo.iinf.polsl.gliwice.pl/~mciura/shellsort.pdf). The increments were found empirically, no formula for them is known.

Implementations

Using a list of shell sizes

The following Java program sorts an array a from index position 0 through n-1. The number of columns used for arranging data in each step is in array cols. Thus, data are arranged in 4,356,424 columns in the first step and in one column in the last step. Note that essentially nothing is done if the number of columns h is larger than the number of data elements n. Each column is sorted by insertion sort. First, data of the second row, beginning at i = h, are sorted to the correct position in their column, then data of the third row (when i reaches value 2h) and so on.

void shellsort (int[] a, int n) {
    int[] cols= { 4356424, 1355339, 543749, 213331, 84801, 27901,
                    11969, 4711, 1968, 815, 277, 97, 31, 7, 3, 1 };
    for (int k = 0; k < 16; k++) {
        int h = cols[k];
        for (int i = h; i < n; i++) {
            int v = a[i];
            int j = i;
            while (j >= h && a[j - h] > v) {
                a[j] = a[j - h];
                j = j - h;
            }
            a[j] = v;
        }
    }
}

Using fibonacci numbers

void shellsort (int[] a, int n) {
    int h = 1, hh = 1;

    while(hh < n) {
       // eg, h = 5, hh = 8
       hh = hh + h; // hh = 8 + 5 = 13
       h = hh - h;  // h = 13 - 5 = 8
    }

    while(hh > 1) {
        for (int i = h; i < n; i++) {
            int v = a[i];
            int j = i;
            while (j >= h && a[j - h] > v) {
                a[j] = a[j - h];
                j = j - h;
            }
            a[j] = v;
        }

        // eg, h = 8, hh = 13

        h = hh - h; // h = 13 - 8 = 5
        hh = hh - h; // hh = 13 - 5 = 8
    }
}

The squares of Fibonacci numbers (1, 4, 9, 25, ...) make an even better sequence.

References

[Kn] D.E. Knuth: Sorting and Searching, vol. 3 of The Art of Computer Programming. Addison-Wesley (1973)
[Se] R. Sedgewick: Algorithms. Addison-Wesley (1988)
[Sh] D.L. Shell: A high-speed sorting procedure. Communications of the ACM 2 (7), 30-32 (1959)

External links

es:Ordenación Shell Sort it:Shell sort lt:Kevalo rūšiavimo algoritmas nl:Shellsort ja:シェルソート zh:希尔排序

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools