All Topics  
Shell sort

 

   Email Print
   Bookmark   Link






 

Shell sort



 
 
Shell sort is a sorting algorithm
Sorting algorithm

In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a List in a certain Total order. The most-used orders are numerical order and lexicographical order....
 that is a generalization of insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort....
, with two observations:

Shell sort is named after its inventor, Donald Shell
Donald Shell

Donald L. Shell is a retired United States computer science who designed the Shell sort sorting algorithm. He acquired his Ph.D. in Mathematics from the University of Cincinnati in 1959, after publishing the shell sort algorithm in the Communications of the ACM in July the same year....
, who published the algorithm in 1959. 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."

Implementation
The original implementation performs O
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
(n2) comparisons and exchanges in the worst case.






Discussion
Ask a question about 'Shell sort'
Start a new discussion about 'Shell sort'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Shell sort is a sorting algorithm
Sorting algorithm

In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a List in a certain Total order. The most-used orders are numerical order and lexicographical order....
 that is a generalization of insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge 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
Donald Shell

Donald L. Shell is a retired United States computer science who designed the Shell sort sorting algorithm. He acquired his Ph.D. in Mathematics from the University of Cincinnati in 1959, after publishing the shell sort algorithm in the Communications of the ACM in July the same year....
, who published the algorithm in 1959. 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."

Implementation


The original implementation performs O
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
(n2) comparisons and exchanges in the worst case. A minor change given in V. Pratt's book improved the bound to O(n log2 n). This is worse than the optimal comparison sort
Comparison sort

A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation that determines which of two elements should occur first in the final sorted list....
s, 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
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
. Using an O(n2) sort such as bubble sort
Bubble sort

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order....
 or insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge 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 Shell sort in the following way: arrange the list into a table and sort the columns (using an insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge 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 82
25 59 94 65 23
45 27 73 25 39
10


We then sort each column, which gives us

10 14 73 25 23
13 27 94 33 39
25 59 94 65 82
45


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

Shellsort
The gap sequence is an integral part of the Shell sort algorithm. Any increment sequence will work, as long as the last increment 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
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort....
, guaranteeing that the final list is sorted.

The gap sequence that was originally suggested by Donald Shell
Donald Shell

Donald L. Shell is a retired United States computer science who designed the Shell sort sorting algorithm. He acquired his Ph.D. in Mathematics from the University of Cincinnati in 1959, after publishing the shell sort algorithm in the Communications of the ACM in July the same year....
 was to begin with and to halve the number until it reaches 1. While this sequence provides significant performance enhancements over the quadratic
Quadratic growth

In mathematics, a function or sequence is said to exhibit quadratic growth when its values are proportionality to the squaring of the function argument or sequence position, in the limit as the argument or sequence position goes to infinity....
 algorithms such as insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort....
, it can be changed slightly to further decrease the average and worst-case running times. Weiss' textbook demonstrates that this sequence allows a worst case 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 Shell sort 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, Shell sort has a proven worst-case running time of (using Shell's increments that start with 1/2 the array size and divide by 2 each time), (using Hibbard's increments of ), (using Sedgewick's increments of , or ), or (using Pratt's increments ), and possibly unproven better running times. The existence of an worst-case implementation of Shell sort was precluded by Poonen, Plaxton, and Suel.

The best known sequence according to research by Marcin Ciura is 1, 4, 10, 23, 57, 132, 301, 701, 1750. This study also concluded that "comparisons rather than moves should be considered the dominant operation in Shellsort." A Shell sort using this sequence runs faster than an insertion sort
Insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort....
 or a heap sort, but even if it is faster than a quicksort
Quicksort

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, average performance, makes comparisons to sort n items. However, in the Best, worst and average case, it makes comparisons....
 for small arrays, it is slower for sufficiently big arrays. After 1750, gaps in geometric progression
Geometric progression

In mathematics, a geometric progression, also known as a geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed non-zero number called the common ratio....
 can be used, such as: nextgap = round(gap * 2.3) //

Another sequence which performs empirically well is the Fibonacci numbers (leaving out one of the starting 1's) to the power of two times the golden ratio
Golden ratio

In mathematics and the arts, two quantities are in the golden ratio if the ratio between the sum of those quantities and the larger one is the same as the ratio between the larger one and the smaller....
, which gives the following sequence: 1, 9, 34, 182, 836, 4025, 19001, 90358, 428481, 2034035, 9651787, 45806244, 217378076, 1031612713, ….

Shell sort algorithm in pseudocode

input: an array a of length n

inc ? round(n/2) while inc > 0 do: for i = inc .. n - 1 do: temp ? a[i] j ? i while j = inc and a[j - inc] > temp do: a[j] ? a[j - inc] j ? j - inc a[j] ? temp inc ? round(inc / 2.2)

External links

  • – graphical demonstration and discussion of Shell sort