You are on page 1of 36

Analysis & Design of Algorithms

Shell Sort • Prepared by:-

Sagar Virani
Assistant Professor

Computer Engineering
Department
VVP Engineering College
Shell Sort
• Founded by Donald Shell and named the sorting algorithm after
himself in 1959.
• It was introduced to improve the efficiency of simple insertion sort.
• It is also known as diminishing insertion sort.
Shell Sort
• In insertion sort, we move elements only one position ahead.
• When an element must be moved far ahead, many shifts are involved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56

Sorted sequence before key = 2

8 10 12 16 20 23 35 43 65 88 2 56 41 27 67 56

2 8 10 12 16 20 23 35 43 65 88 56 41 27 67 56

Total shifts: 10
Shell Sort
• The shell sort compares elements far apart, then elements less far
apart and finally comparing adjacent elements.
• By starting with far apart elements, it can move some out-of-place
elements into position faster than a simple nearest neighbor exchange.
• It avoids large shifts, if the smaller values is to the far right and must
be moved to the far left.
Shell Sort
• It breaks the original list into several smaller sublist, each of which is
sorted using insertion sort.
• Shell sort uses incremental i, called the gap or interval to create
sublist by choosing all elements that are i elements apart.
• It then progressively reduces the gap between elements to be
compared.
Shell Sort
• There are many ways of choosing the next gap.
• The running time of Shellsort is heavily dependent on the gap
sequence it uses.
• Gap Sequence:
•   … 1 Originally used by Shell.
• 2k - 1 15, 7, 3, 1
• (3k – 1) / 2 13, 4, 1
Shell Sort
List

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56
Shifts
<20, 23> <20, 23>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
<8, 56> <8, 56>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
<8, 56> <8, 56>
<10, 41> <10, 41>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
<8, 56> <8, 56>
<10, 41> <10, 41>
<16, 27> <16, 27>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
<8, 56> <8, 56>
<10, 41> <10, 41>
<16, 27> <16, 27>
<43, 67> <43, 67>
Shell Sort
N = 16 Therefore, gap = 16 / 2 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 23> <20, 23>
<12, 88> <12, 88>
<65, 2> <2, 65> 1
<8, 56> <8, 56>
<10, 41> <10, 41>
<16, 27> <16, 27>
<43, 67> <43, 67>
<35, 56> <35, 56>
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

20 12 2 8 10 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 88 65 56 41 27 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
<12, 16, 88, 27> <12, 16, 27, 88> 1
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 27 65 56 41 88 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
<12, 16, 88, 27> <12, 16, 27, 88> 1
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 27 65 56 41 88 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
<12, 16, 88, 27> <12, 16, 27, 88> 1
<2, 43, 65, 67> <2, 43, 65, 67>
Shell Sort
Now, gap = 16 / 4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 27 65 56 41 88 67 56
Shifts
<20, 10, 23, 41> <10, 20, 23, 41> 1
<12, 16, 88, 27> <12, 16, 27, 88> 1
<2, 43, 65, 67> <2, 43, 65, 67>
<8, 35, 56, 56> <8, 35, 56, 56>

Total Shits: 1 + 2 = 3
Shell Sort
Now, gap = 16 / 8 = 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 27 65 56 41 88 67 56
Shell Sort
Now, gap = 16 / 8 = 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

10 12 2 8 20 16 43 35 23 27 65 56 41 88 67 56
Shifts
<10, 2, 20, 43, 23, 65, 41, 67> <2, 10, 20, 23, 41, 43, 65, 67>
Shell Sort
Now, gap = 16 / 8 = 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

2 12 10 8 20 16 23 35 41 27 43 56 65 88 67 56
Shifts
<10, 2, 20, 43, 23, 65, 41, 67> <2, 10, 20, 23, 41, 43, 65, 67> 4
Shell Sort
Now, gap = 16 / 8 = 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

2 12 10 8 20 16 23 35 41 27 43 56 65 88 67 56
Shifts
<10, 2, 20, 43, 23, 65, 41, 67> <2, 10, 20, 23, 41, 43, 65, 67> 4
<12, 8, 16, 35, 27, 56, 88, 56> <8, 12, 16, 27, 35, 56, 56, 88>
Shell Sort
Now, gap = 16 / 8 = 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

2 8 10 12 20 16 23 27 41 35 43 56 65 56 67 88
Shifts
<10, 2, 20, 43, 23, 65, 41, 67> <2, 10, 20, 23, 41, 43, 65, 67> 4
<12, 8, 16, 35, 27, 56, 88, 56> <8, 12, 16, 27, 35, 56, 56, 88> 3

Total Swaps: 3 + 7 = 10
Shell Sort
Now, gap = 16 / 16 = 1 (Normal Insertion sort)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

2 8 10 12 20 16 23 27 41 35 43 56 65 56 67 88

2 8 10 12 16 20 23 27 41 35 43 56 65 56 67 88

1 1

2 8 10 12 16 20 23 27 35 41 43 56 56 65 67 88

Total Shifts: 10 + 3 = 13
Shell Sort
Advantages of Shell Sort
• Shell sort is an optimization of insertion sort.
• Only efficient for medium size lists.
• For bigger lists, the algorithm is not the best choice.
• Fastest of all O(n2) sorting algorithms.
• 5 times faster than the bubble sort and a little over twice as fast as the insertion
sort, its closest competitor.
Shell Sort
Disadvantages of Shell Sort
• Complex algorithm and its not nearly as efficient as the merge, heap and quick sort.
• Significantly slower than the merge, heap, and quick sort.

When to use it?


• Simple algorithm makes it a good choice for sorting lists of less than 5000 items
unless speed important.
• It's also an excellent choice for repetitive sorting of smaller lists.
Shell Sort
Algorithm
SHELL-SORT(A)
for gap = (A.length / 2) downto 1
INSERTION-SORT( A, gap)
gap = gap / 2
Shell Sort Algorithm Analysis

• The running time of Shell sort is heavily dependent on the gap


sequence it uses.
• If we use, the gap sequence originally proposed by Shell, the running
time is same as Insertion sort.
• Best case: Ω(n)
• Average case: θ(n2)
• Worst case: O(n2)
Pseudo code for Shell sort Algorithm

SHELL-SORT (A)
{
gap = A.length / 2
while gap > 0
for j  gap to n
key = array[ j + 1 ]
i = j – gap + 1
while i > 0 and A[ i ] > key
A[ i + gap ] = A[ i ]
i = i – gap
A[ i ] = key
gap = gap / 2
Any Questions?

You might also like