Shell sort
From Free net encyclopedia
←Older revision | Newer revision→
Shell sort is a sorting algorithm that requires fewer than O(n²) comparisons and exchanges, on average. Although it is easy to develop an intuitive sense of how this algorithm works, it is very difficult to analyze its execution time, but estimates range from O(n1.25) to O(n1.5) depending on implementation details.
Shell sort is a generalization of insertion sort, with two observations in mind:
- Insertion sort is efficient if the input is "almost sorted".
- Insertion sort is inefficient, on average, because it moves values just one position at a time.
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²) 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.
The Shell sort is named after its inventor, Donald Shell, who published it in 1959. Some older textbooks and references incorrectly call this the "Shell-Metzner" sort, but that is an incorrect naming.
Contents |
The Gap sequence
Image:Shellsort.png The gap sequence is an integral part of the shellsort algorithm. Any increment sequence will work, so 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 effectively an Insertion sort, guaranteeing that the final list is sorted. Thus, if the gap sequence does not terminate with 1, we cannot be sure that our final result is sorted.
The gap sequence that was originally suggested by Donald Shell was to begin with <math>N/2</math> and half 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.
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.
Example implementation in C
/* * Performs an insertion sort on elements of a[] with the given gap. * If gap == 1, performs an ordinary insertion sort. * If gap >= length, does nothing. * Based on a C implementation from the article Insertion sort implementations */ void shellSortPhase(int a[], int length, int gap) { int i; for (i = gap; i < length; ++i) { int value = a[i]; int j; for (j = i - gap; j >= 0 && a[j] > value; j -= gap) { a[j + gap] = a[j]; } a[j + gap] = value; } } void shellSort(int a[], size_t length) { /* * gaps[] should approximate a geometric progression. * The following sequence is the best known in terms of * the average number of key comparisons made [1] */ static const int gaps[] = { 1, 4, 10, 23, 57, 132, 301, 701 }; int sizeIndex; for (sizeIndex = sizeof(gaps)/sizeof(gaps[0]) - 1; sizeIndex >= 0; --sizeIndex) shellSortPhase(a, length, gaps[sizeIndex]); }
References
External links
- Detailed analysis of Shell sort
- Analysis of Shellsort and Related Algorithms, Robert Sedgewick
- Dictionary of Algorithms and Data Structures: Shellsort
Template:Comp-sci-stubde:Shellsort
es:Ordenación Shell Sort
it:Shell sort
lt:Šelo rūšiavimo algoritmas
nl:Shellsort
ja:シェルソート
pl:Sortowanie Shella
pt:Shell sort
zh:希尔排序