A diminishing gap sort is another name for Shellsort

Draw Code 39 In JavaUsing Barcode drawer for Java Control to generate, create Code 39 image in Java applications.

Shell's increment sequence is an improvement over the insertion sort (although better sequences are known)

Make Barcode In JavaUsing Barcode drawer for Java Control to generate, create barcode image in Java applications.

Shellsort after each pass if the increment sequence is { I , 3, 5)

Scan Barcode In JavaUsing Barcode decoder for Java Control to read, scan read, scan image in Java applications.

Sorting Algorithms

Paint USS Code 39 In C#.NETUsing Barcode maker for VS .NET Control to generate, create Code 3/9 image in .NET framework applications.

Shellsort

Code 39 Full ASCII Printer In .NET FrameworkUsing Barcode encoder for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.

10,000 20,000

Encode Code 3/9 In Visual Studio .NETUsing Barcode creation for .NET framework Control to generate, create Code 39 image in Visual Studio .NET applications.

Insertion Sort

Code 39 Extended Encoder In Visual Basic .NETUsing Barcode creator for .NET Control to generate, create Code 39 image in Visual Studio .NET applications.

575 2,489

Drawing Code 39 In JavaUsing Barcode printer for Java Control to generate, create Code 39 Full ASCII image in Java applications.

Shell's Increments

Drawing Universal Product Code Version A In JavaUsing Barcode printer for Java Control to generate, create UPCA image in Java applications.

10 23

USS Code 128 Printer In JavaUsing Barcode printer for Java Control to generate, create Code 128C image in Java applications.

Odd gaps only

Printing Bar Code In JavaUsing Barcode creation for Java Control to generate, create bar code image in Java applications.

11 23

Print UCC-128 In JavaUsing Barcode maker for Java Control to generate, create GS1 128 image in Java applications.

Dividing by 22

Leitcode Encoder In JavaUsing Barcode creation for Java Control to generate, create Leitcode image in Java applications.

9 20

Painting Code 3/9 In VB.NETUsing Barcode creator for Visual Studio .NET Control to generate, create USS Code 39 image in .NET framework applications.

Running time (milliseconds) of the insertion sort and Shellsort for various increment sequences

Bar Code Encoder In VB.NETUsing Barcode printer for VS .NET Control to generate, create barcode image in .NET applications.

summary of Shellsort's performance with three different choices of increment sequences is shown in Figure 95

Paint Barcode In Visual C#Using Barcode printer for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.

Performance of Shellsort

Bar Code Drawer In VS .NETUsing Barcode generation for .NET Control to generate, create bar code image in VS .NET applications.

In the worst case, Shell's increments give quadratic behavior

Bar Code Decoder In JavaUsing Barcode decoder for Java Control to read, scan read, scan image in Java applications.

The running time of Shellsort depends heavily on the choice of increment sequences, and in general the proofs can be rather involved The averagecase analysis of Shellsort is a long-standing open problem except for the most trivial increment sequences When Shell's increments are used, the worst case is O(N2) This bound is achievable if N is an exact power of 2, all the large elements are in evenindexed array positions, and all the small elements are in odd-indexed array positions When the final pass is reached, all the large elements will still be in the even-indexed array positions, and all the small elements will still be in the odd-indexed array positions A calculation of the number of remaining inversions shows that the final pass will require quadratic time The fact that this is the worst that can happen follows from the fact that an hk-sort consists of hk insertion sorts of roughly Nthk elements Thus the cost of each pass is O(hk(Nlhk)2), O(N21hk)When we sum this cost over all the passes, we or obtain O(N2Chk)The increments are roughly a geometric series, so the sum is bounded by a constant The result is a quadratic worst-case running time We can also prove via a complex argument that when N is an exact power of 2 the average running time is O(N312) Thus, on average, Shell's increments give a significant improvement over insertion sort

Generating Code 3/9 In .NETUsing Barcode printer for .NET Control to generate, create Code 39 image in Visual Studio .NET applications.

Shellsort

Generate Code 3/9 In Visual C#Using Barcode drawer for .NET Control to generate, create Code 39 Extended image in .NET applications.

1 / / Shellsort 2 template <class Comparable> 3 void shellsort( vector<Comparable> & a ) 4 I 5 for( int gap = asize( ) / 2; gap > 0; 6 gap = gap == 2 1 : static-cast<int>( gap / 22 7 for( int i = gap; i < asize( ) ; i++ )

8 9 1 0

11 12 13 14

Comparable tmp = a[ i 1 ; int j = i; for( ; j > = g a p && tmp < a [ j - gap I ; j - = g a p a[ j I = a [ j - g a p I ; a[ j ] = tmp;

Shellsort implementation

A minor change to the increment sequence can prevent the quadratic worst case from occurring If we divide gap by 2 and it becomes even, we can add 1 to make it odd We can then prove that the worst case is not quadratic but only O(N3/2)Although the proof is complicated, the basis for it is that in this new increment sequence, consecutive increments share no common factors (whereas in Shell's increment sequence they do) Any sequence that satisfies this property (and whose increments decrease roughly geometrically) will have a worst-case running time of at most O(N3/')2 The average performance of the algorithm with these new increments is unknown but seems to be O(N5/4), based on simulations A third sequence, which performs well in practice but has no theoretical basis, is to divide by 22 instead of 2 This divisor appears to bring the average running time to below O(N514)-perhaps to O(N7/6)-but this case is completely unresolved For 100,000 to 1,000,000 items, it typically improves performance by about 25 to 35 percent over Shell's Increments, although nobody knows why A Shellsort implementation with this increment sequence is coded in Figure 96 The complicated code at line 6 is necessary to avoid setting gap to 0 If that happens, the algorithm is broken because we never see a 1-sort Line 6 ensures that, if gap is about to be set to 0, it is reset to 1 3