# CSCD 300 Data Structures Donald Shell¶s Sorting Algorithm

Originally developed by Bill Clark, modified by Tom Capaul and Tim Rolfe

1

7 (July 1959). Communications of the ACM Vol 2. ³A High-Speed Sorting Procedure´. 2 .Shell Sort . Shell¶s Sort Created in 1959 by Donald Shell Link to a local copy of the article: Donald Shell. 30-32 Originally Shell built his idea on top of Bubble Sort (link to article flowchart). No.Introduction More properly. but it has since been transported over to Insertion Sort.

Shell Sort -General Description Essentially a segmented insertion sort Divides an array into several smaller noncontiguous segments The distance between successive elements in one segment is called a gap. Then resegment into larger segments (smaller gaps) and repeat sort. 3 . Continue until only one segment (gap = 1) .final sort finishes array sorting. Each segment is sorted within itself using insertion sort.

Insertion sort is fastest when: The array is nearly sorted. Shell sort works well because: It always deals with a small number of elements. The array contains only a small number of data items. Elements are moved a long way through array with each swap and this leaves it more nearly sorted.Shell Sort -Background General Theory: Makes use of the intrinsic strengths of Insertion sort. 4 .

example Initial Segmenting Gap = 4 80 93 60 12 42 30 68 85 10 10 30 60 12 42 93 68 85 80 5 .Shell Sort .

example (2) Resegmenting Gap = 2 10 30 60 12 42 93 68 85 80 10 12 42 30 60 85 68 93 80 6 .Shell Sort .

Shell Sort .example (3) Resegmenting Gap = 1 10 12 42 30 60 85 68 93 80 10 12 30 42 60 68 80 85 93 7 .

. An array which is hk sorted and is then hk-1 sorted remains hk sorted. h2.. Any sequence will work as long as it is increasing and h1 = 1. 8 . h3. For any gap value hk we have A[i] <= A[i + hk] An array A for which this is true is hk sorted. ht is a sequence of increasing integer values which will be used as a sequence (from right to left) of gap values.Gap Sequences for Shell Sort The sequence h1. . .

Shell Sort . Obtaining a relatively prime sequence is often not practical in a program so practical solutions try to approximate relatively prime sequences.Ideal Gap Sequence Although any increasing sequence will work ( if h1 = 1): Best results are obtained when all values in the gap sequence are relatively prime (sequence does not share any divisors). 9 .

10 . 2.most nearly a relatively prime sequence.successive gaps are previous value divided by 2. Odd gaps only .2 and truncate. best performance of all .Practical Gap Sequences Three possibilities presented: 1) Shell's suggestion .like Shell method except if division produces an even number add 1.first gap is N/2 .like Odd gaps method (add 1 to even division result) but use a divisor of 2.2 method . better performance than 1) since all odd values eliminates the factor 2.Shell Sort .

recommended another sequence of gaps.Shell Sort . h0 = 1 hj+1 = hj * 3 + 1 Find the hj > n. then start with hj/3 11 .Added Gap Sequence Donald Knuth. in his discussion of Shell¶s Sort.

12 .Link to the Java program that generated the above data.

Shell Sort .Time Complexity Time complexity: O(nr) with 1 < r < 2 This is better than O(n2) but generally worse than O(n log2n). 13 .

int j = i.Code public static void shellSort( Comparable[ ] theArray. gap = gap / 2 ) for( int i = gap. j >= gap && tmp. i++ ) { Comparable tmp = theArray[ i ]. for( . } } 14 . gap > 0. theArray[ j ] = tmp.gap ]. i < n.Shellsort . int n ) { // shellSort: sort first n items in array theArray for( int gap = n / 2. j -= gap ) theArray[ j ] = theArray[ j .compareTo(theArray[ j .gap ]) < 0 .

i++ ) { Comparable tmp = theArray[ i ]. } 15 . i < n. j -= gap ) theArray[ j ] = theArray[ j . theArray[ j ] = tmp. int j = i.gap ]. gap = gap / 2 ) for( int i = gap.compareTo(theArray[ j .ShellSort -Trace (gap = 4) [0] [1] [2] [3] 12 i: j: [4] [5] 42 30 [6] 68 [7] 85 [8] 10 theArray 80 93 60 n: 9 gap: 4 for( int gap = n / 2.gap ]) < 0 . for( . j >= gap && tmp. gap > 0.

i++ ) { Comparable tmp = theArray[ i ]. for( .gap ]. i < n.ShellSort -Trace (gap = 2) [0] theArray 10 n: 9 gap: 2 [1] [2] [3] 12 i: j: [4] [5] 42 93 [6] 68 [7] 85 [8] 80 30 60 for( int gap = n / 2. gap > 0.compareTo(theArray[ j . } 16 . j >= gap && tmp. int j = i. theArray[ j ] = tmp. j -= gap ) theArray[ j ] = theArray[ j . gap = gap / 2 ) for( int i = gap.gap ]) < 0 .

int j = i. } 17 .ShellSort -Trace (gap = 1) [0] theArray 10 n: 9 gap: 1 [1] [2] [3] 30 i: j: [4] [5] 60 85 [6] 68 [7] 93 [8] 80 12 42 for( int gap = n / 2. i < n. j -= gap ) theArray[ j ] = theArray[ j .gap ].compareTo(theArray[ j . gap = gap / 2 ) for( int i = gap. j >= gap && tmp.gap ]) < 0 . gap > 0. i++ ) { Comparable tmp = theArray[ i ]. for( . theArray[ j ] = tmp.