Sei sulla pagina 1di 11

Shell Sort

Shell sort is a substantial improvement over


insertion sort
The elements move in long strides rather than
single steps, thereby yielding a comparatively
short sub file or a comparatively well ordered
sub file which quickens the sorting process
The shell sort procedure was proposed by
Donald L Shell in 1959
Shell Sort
Choose an increment ht and divide a list of
unordered keys L={K1,K2,K3,Kj,Kn} into sublists
of keys that are ht units apart
Each of the sub lists are individually sorted and
gathered to form a list. This is known as a pass
Now we repeat the pass for any sequence of
increments {ht-1,ht-2,.h2,h1,h0} where h0 must
equal 1
The increments are kept in the diminishing order
and therefore shell sort is also referred to as
diminishing increment sort
Shell Sort
Example illustrates shell sort on the given list L for
an increment sequence {8,4,2,1}
Trace the shell sort procedure on the unordered
list L of keys given by
L={24,37,46,11,85,47,33,66,22,84,95,55,14,09,76
,35} for an increment sequence
{h3, h2, h1, h0}={8,4,2,1}
The steps traced are,
Pass 1 for an increment 8, divides the unordered
list L into 8 sublists each comprising 2 keys, that
are 8 units apart
Shell Sort
After each of the sublists have been
individually insertion sorted, they are
gathered together for the next pass
In pass 2, for an increment 4, the list gets
divided into 4 groups, each comprising
elements which are 4 units apart in the list L
The individual sub lists are again insertion
sorted and gathered together for the next
pass and so on, until in Pass 4 the entire list
gets sorted for an increment 1
Shell Sort
Unordered list L:
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

24 37 46 11 85 47 33 66 22 84 95 55 14 09 76 35

Pass 1 (increment h3=8)


K1 K9 K2 K10 K3 K11 K4 K12 K5 K13 K6 K14 K7 K15 K8 K16

24 22 37 84 46 95 11 55 85 14 47 09 33 76 66 35

After insertion sort


(22 24) (37 84) (46 95) (11 55) (14 85) (09 47) (33 76) (35 66)
Shell Sort
List L after Pass 1
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

22 37 46 11 14 09 33 35 24 84 95 55 85 47 76 66

Pass 2 (increment h2=4)


K1 K5 K9 K13 K2 K6 K10 K14 K3 K7 K11 K15 K4 K8 K12 K16

22 14 24 85 37 09 84 47 46 33 95 76 11 35 55 66

After insertion sort


(14 22 24 85) (09 37 47 84) (33 46 76 95) (11 35 55 66)
Shell Sort
List L after Pass 2
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

14 09 33 11 22 37 46 35 24 47 76 55 85 84 95 66

Pass 3 (increment h1=2)


K1 K3 K5 K7 K9 K11 K13 K15 K2 K4 K6 K8 K10 K12 K14 K16

14 33 22 46 24 76 85 95 09 11 37 35 47 55 84 66

After insertion sort

(14 22 24 33 46 76 85 95) (09 11 35 37 47 55 66 84)


Shell Sort
List L after Pass 3
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

14 09 22 11 24 35 33 37 46 47 76 55 85 66 95 84

Pass 4 (increment h0=1)


K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

14 09 22 11 24 35 33 37 46 47 76 55 85 66 95 84

After insertion sort


(09 11 14 22 24 33 35 37 46 47 55 66 76 84 85 95)

Sorted List L
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12 K13 K14 K15 K16

09 11 14 22 24 33 35 37 46 47 55 66 76 84 85 95
Shell Sort
Trace the shell sort procedure on the
unordered list L of keys given by
L={24,37,46,11,85,47,33,66,22,84,95,55,14,09
,76,35} for an increment sequence
{h3, h2, h1, h0}={7,5,3,1}
Shell Sort
Algorithm for Shell sort
Procedure shell_sort(L,n,H)
/* L[1:n] is the unordered list of keys to be shell sorted.
(L={K1,K2,Kj,..Kn}) H={ht,ht-1,ht-2,h2,h1,h0) is the
sequence of increments */
For each hj H do
Insertion sort the sublist of elements in L[1:n]
Which are hj units apart, such that
L[i]<= L[i+hj ], for 1<=i<=N-hj
End
Print(L)
End Shell_sort
Analysis of Shell Sort
The analysis of shell sort is dependent on a
given choice of increments
Since there is no best possible sequence of
increments that has been formulated,
especially for large values of n ( the size of the
list L), the time complexity of shell sort is
completely resolved

Potrebbero piacerti anche