0% found this document useful (0 votes)
13 views

Shell Sort: Group 1-Dexedrine Bscs-Cs Ii-1

The shell sort is an improved version of insertion sort that uses diminishing increments to sort data. It works by dividing the list into segments based on an increment value, then comparing adjacent keys within each segment. The increment is reduced in each pass until the final pass where it is 1, performing standard insertion sort. Good performance comes from exchanges of elements far apart, unlike insertion sort which only exchanges adjacent elements.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Shell Sort: Group 1-Dexedrine Bscs-Cs Ii-1

The shell sort is an improved version of insertion sort that uses diminishing increments to sort data. It works by dividing the list into segments based on an increment value, then comparing adjacent keys within each segment. The increment is reduced in each pass until the final pass where it is 1, performing standard insertion sort. Good performance comes from exchanges of elements far apart, unlike insertion sort which only exchanges adjacent elements.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Shell Sort

Group 1-Dexedrine
BSCS-CS II-1
Shell sort
 The shell sort is an improved version of the
straight insertion sort in which diminishing
partitions are used to sort the data.
 In the shell sort, a list of N elements is

divided into K segments, where K is known as


the increment.
 To compare adjacent keys in a segment, we
add the increment to the current index, as
shown below.
list[cur]: list[cur + incre]
 After each pass through the data, the
increment is reduced until, in the final pass, it
is 1.
 Although the only absolute is that the last
increment must be 1, the size of the
increments influences the efficiency of the sort.
How Shell sort qualifies as Insertion
sort
 Recall that insertion sorts insertion new data
into their correct location in the ordered
portion of the list.
 This concept is found in the last part of shell

sort, where increment is equal to one.


Selecting good values for n

 Mathematically proven that using only


numbers from the power series {1, 2, 4, 8,
16, 32, ...} produces the worst sorting times.
The fastest times are (on average) obtained
by choosing an initial n somewhere close to
the maximum allowed and continually
dividing by 2.2 until you reach 1 or less.
Remember to always sort the data with n = 1
as the last step.
Example:

 We have chosen the sequence {3, 2, 1} for


our n.
 This is less than the maximum (which in this

case is 4, because this is the largest number


less than half the number of elements we
have).
N=3
N=1(insertion)
 Shell sort speed comes from the fact that it
exchanges elements that are far apart (the
insertion sort exchanges only adjacent
elements).
Pseudocode
inc ← round(n/2)
while inc > 0 do:
for i = inc .. n − 1 do:
  temp ← a[i] 
j ← i 
while j ≥ inc and a[j − inc] > temp do: 
a[j] ← a[j − inc]
 j ← j − inc 
a[j] ← temp 
inc ← round(inc / 2)
Origin
 The Shell sort is named after its inventor, 
Donald Shell, who published the algorithm in
1959.

You might also like