Shell Sort

Embed Size (px)

Text of Shell Sort

CSCD 300 Data Structures Donald Shells Sorting AlgorithmOriginally developed by Bill Clark, modified by Tom Capaul and Tim Rolfe

1

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

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

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

Shell Sort - exampleInitial 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 - example (3)Resegmenting Gap = 1 10 12 42 30 60 85 68 93 80

10

12 30

42

60 68

80

85

93

7

Gap Sequences for Shell SortThe sequence h1, h2, h3,. . . , ht is a sequence of increasing integer values which will be used as a sequence (from right to left) of gap values.Any sequence will work as long as it is increasing and h1 = 1.

For any gap value hk we have A[i]