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:

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 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.

The following method shows how to implement shell sort with Java:

 ``` /** * Shellsort, using a sequence suggested by Gonnet. * @param a an array of Comparable items. */ public static void shellsort( Comparable [ ] a ) { for( int gap = a.length / 2; gap > 0; gap = gap == 2 ? 1 : (int) ( gap / 2.2 ) ) for( int i = gap; i < a.length; i++ ) { Comparable tmp = a[ i ]; int j = i; for( ; j >= gap && tmp.compareTo( a[ j - gap ] ) < 0; j -= gap ) a[ j ] = a[ j - gap ]; a[ j ] = tmp; } } ```