Shell sort

Shell sort

Shell sort is a sorting algorithm that is a generalization of insertion sort, with two observations:
*insertion sort is efficient if the input is "almost sorted", and
*insertion sort is typically inefficient because it moves values just one position at a time.

History

The Shell sort is named after its inventor, Donald Shell, who published the algorithm in 1959. [cite journal |last=Shell |first=D.L. |title=A high-speed sorting procedure |journal=Communications of the ACM |volume=2 |issue=7 |year=1959 |pages=30–32 |doi=10.1145/368370.368387] Some older textbooks and references call this the "Shell-Metzner" sort after Marlene Metzner Norton, but according to Metzner, "I had nothing to do with the sort, and my name should never have been attached to it." [cite web |title=Shell sort |publisher=National Institute of Standards and Technology |url=http://www.nist.gov/dads/HTML/shellsort.html |accessdate=2007-07-17 ]

Implementation

The original implementation performs O("n"2) comparisons and exchanges in the worst case. A minor change given in V. Pratt's book [Cite book|last=Pratt|first=V|year=1979|publisher=Garland|title=Shellsort and sorting networks (Outstanding dissertations in the computer sciences)|id=ISBN 0-824-04406-1 (This was originally presented as the author's Ph.D. thesis, Stanford University, 1971)] improved the bound to O("n" log2 "n"). This is worse than the optimal comparison sorts, which are O("n" log "n").

Shell sort improves insertion sort by comparing elements separated by a gap of several positions. This lets an element take "bigger steps" toward its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. The last step of Shell sort is a plain 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 an O("n"2) sort such as bubble sort or insertion sort, it will take roughly "n" comparisons and exchanges to move this value all the way to the other end of the array. Shell sort first moves 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.

One can visualize Shellsort in the following way: arrange the list into a table and sort the columns (using an insertion sort). Repeat this process, each time with smaller number of longer columns. At the end, the table has only one column. While transforming the list into a table makes it easier to visualize, the algorithm itself does its sorting in-place (by incrementing the index by the step size, i.e. using i += step_size instead of i++).

For example, consider a list of numbers like [ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ] . If we started with a step-size of 5, we could visualize this as breaking the list of numbers into a table with 5 columns. This would look like this:

13 14 94 33 8225 59 94 65 2345 27 73 25 3910

We then sort each column, which gives us

10 14 73 25 2313 27 94 33 3925 59 94 65 8245

When read back as a single list of numbers, we get [ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ] . Here, the 10 which was all the way at the end, has moved all the way to the beginning. This list is then again sorted using a 3-gap sort, and then 1-gap sort (simple insertion sort).

Gap sequence

The "gap sequence" is an integral part of the shellsort algorithm. Any increment sequence will work, as long as the last element is 1. The algorithm begins by performing a "gap insertion sort", with the gap being the first number in the gap sequence. It continues to perform a gap insertion sort for each number in the sequence, until it finishes with a gap of 1. When the gap is 1, the gap insertion sort is simply an ordinary insertion sort, guaranteeing that the final list is sorted.

The gap sequence that was originally suggested by Donald Shell was to begin with N/2 and to halve the number until it reaches 1. While this sequence provides significant performance enhancements over the quadratic algorithms such as insertion sort, it can be changed slightly to further decrease the average and worst-case running times. Weiss' textbook [Cite book|last=Weiss|first=Mark Allen|year=2002|publisher=Addison Wesley|title=Data Structures & Problem Solving using Java|id=ISBN 0-201-74835-5] demonstrates that this sequence allows a worst case O(n^2) sort, if the data is initially in the array as (small_1, large_1, small_2, large_2, ...) - that is, the upper half of the numbers are placed, in sorted order, in the even index locations and the lower end of the numbers are placed similarly in the odd indexed locations.

Perhaps the most crucial property of Shellsort is that the elements remain k-sorted even as the gap diminishes. For instance, if a list was 5-sorted and then 3-sorted, the list is now not only 3-sorted, but both 5- and 3-sorted. If this were not true, the algorithm would undo work that it had done in previous iterations, and would not achieve such a low running time.

Depending on the choice of gap sequence, Shellsort has a proven worst-case running time of O(n^2) (using Shell's increments that start with 1/2 the array size and divide by 2 each time), O(n^{3/2}) (using Hibbard's increments of 2^k-1), O(n^{4/3}) (using Sedgewick's increments of 9 imes4^i - 9 imes2^i + 1, or 4^{i+1} + 3 imes2^i + 1), or O(nlog^2 n) (using Pratt's increments 2^i3^j), and possibly unproven better running times. The existence of an O(n log n) worst-case implementation of Shellsort was precluded by Poonen, Plaxton, and Suel [Cite journal|last=Poonen, Plaxton, Suel|title=Improved Lower Bounds for Shellsort|journal=Annual Symposium on Foundations of Computer Science|year=1992|issue=33|pages=226–235] .

The best known sequence is 1, 4, 10, 23, 57, 132, 301, 701, 1750. Such a Shell sort is faster than an insertion sort and a heap sort,vague|date=March 2008 but if it is faster than a quicksort for small arrays (less than 50 elements), it is slower for bigger arrays.Fact|date=March 2008 After 1750, gaps in geometric progression can be used, such as: nextg

Shell sort algorithm in C/C++

The following is an implementation of shell sort written in C/C++ for sorting an array of integers. The increment sequence used in this example code gives an O(n2) worst-case running time.

void shell_sort(int A [] , int size) { int i, j, increment, temp; increment = size / 2; while (increment > 0) { for (i = increment; i < size; i++) { j = i; temp = A [i] ; while ((j >= increment) && (A [j-increment] > temp)) { A [j] = A [j - increment] ; j = j - increment; } A [j] = temp; } if (increment = 2) increment = 1; else increment = (int) (increment / 2.2);

hell sort algorithm in Java

A Java implementation of Shell sort is as follows:public static void shellSort(int [] a) { for (int increment = a.length / 2; increment > 0; increment = (increment = 2 ? 1 : (int) Math.round(increment / 2.2))) { for (int i = increment; i < a.length; i++) { int temp = a [i] ; int j; for (j = i; j >= increment && a [j - increment] > temp; j -= increment){ a [j] = a [j - increment] ; } a [j] = temp; }

hell sort algorithm in Python

def shellsort(seq): def increment_generator(skip): while skip != 1: skip = 1 if skip = 2 else 5 * skip // 11 yield skip

for increment in increment_generator(len(seq)): for i in xrange(increment, len(seq)): for j in xrange(i, increment-1, -increment): if seq [j - increment] < seq [j] : break # swap seq [j] with seq [j - increment] seq [j] , seq [j - increment] = seq [j - increment] , seq [j]

# Usage demo:alist = [5, 2, 7, -4, 6, 2, 0, 11] shellsort(alist)print alist

The function doesn't return the array to denote it mutates the input. That script prints: [-4, 0, 2, 2, 5, 6, 7, 11]

References

External links

* [http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/shell/shellen.htm Detailed analysis of Shell sort]
* [http://www.cs.princeton.edu/~rs/shell/ Analysis of Shellsort and Related Algorithms, Robert Sedgewick]
* [http://www.research.att.com/~njas/sequences/A102549 Best known gap sequence]
* [http://vision.bc.edu/~dmartin/teaching/sorting/anim-html/shell.html A graphical demonstration and discussion of shell sort]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Ordenación Shell Sort — El Shell Sort es un ordenamiento de disminución incremental, nombrado así debido a su inventor Donald Shell. Ordena subgrupos de elementos separados K unidades (respecto de su posición en el arreglo) del arreglo original. El valor K es llamado… …   Enciclopedia Universal

  • Shell — may refer to * Animal shell, or exoskeleton, including those of molluscs, turtles, insects and crustaceans * Seashell, the shells of various marine animals, especially marine mollusks * Eggshell, the outer covering of a hard shelled eggAny more… …   Wikipedia

  • sort — UNIX‐утилита, выводящая сортированное слияние указанных файлов на стандартный вывод с использованием установленной в среде локали. Использование sort [ m][ o output][ bdfinru][ t char][ k keydef]… [file…] sort c [ bdfinru][ t char][ k… …   Википедия

  • Sort (Unix) — Pour les articles homonymes, voir sort. sort est une commande POSIX qui permet de trier les lignes de fichiers texte. Par défaut, sort affiche l ensemble des lignes des fichiers qu on lui passe en paramètre triées par ordre croissant de la table… …   Wikipédia en Français

  • Shell (projectile) — This article is about the artillery projectile. For the small arms ammunition, see Shotgun shell. Some sectioned shells from the First World War. From left to right: 90 mm fragmentation shell, 120 mm pig iron incendiary shell, 77/14 model 75 mm… …   Wikipedia

  • Shell (computing) — A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is… …   Wikipedia

  • Shell to Sea — Members of An Garda Síochána and Shell To Sea campaigners scuffle over ownership of a road (June 2007) Shell to Sea (in Irish, Shell chun Sáile) is a campaign based in Cill Chomáin parish, Erris, County Mayo, Ireland which opposes the proposed… …   Wikipedia

  • Shell Shaker — Infobox Book | name = Shell Shaker image caption = author = LeAnne Howe cover artist = Jaune Quick to See Smith country = United States language = English genre = novel publisher = Aunt Lute Books pub date = September 2001 media type = Print… …   Wikipedia

  • shell collecting — ▪ hobby       practice of finding and usually identifying the shells of mollusks, a popular avocation, or hobby, in many parts of the world. These shells, because of their bright colours, rich variety of shapes and designs, and abundance along… …   Universalium

  • sort (Unix) — Pour les articles homonymes, voir sort. sort est une commande POSIX qui permet de trier des fichiers ou leurs contenus. Par défaut, sort affiche l ensemble des lignes des fichiers qu on lui passe en paramètre triées par ordre croissant de la… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”