Shell sort is a sorting algorithm that requires asymptotically fewer than O(n²) comparisons and exchanges in the worst case. 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(nlog2 n) to O(n1.5) depending on implementation details.
Shell sort is a generalization of insertion sort, with two observations in mind:
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 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." *
The gap sequence that was originally suggested by Donald Shell was to begin with 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.
Depending on the choice of gap sequence, Shellsort has a worst-case running time of , , , or . The existence of an worst-case implementation of Shellsort remains an open research question.
import Data.List (transpose)
-- Insertion sort, for sorting columns.
insert :: Ord a => a -> -> [a
insert x = [x
insert x (y:ys) | x <= y = x : y:ys
| otherwise = y : insert x ys
insertionSort :: Ord a => -> [a
insertionSort = foldr insert *
-- Splits a list into k columns.
columnize :: Int -> * ->
Sort algorithms | Comparison sorts
Shellsort | Ordenación Shell Sort | Tri de Shell | Shell sort | Šelo rūšiavimo algoritmas | Shellsort | シェルソート | Sortowanie Shella | Shell sort | Сортировка методом Шелла | 希尔排序
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Shell sort".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world