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

Shell Sort 21

The document provides an overview of Shell Sort, a sorting algorithm that improves upon insertion sort by allowing the exchange of far items, thus reducing the number of swaps and comparisons. It outlines the steps involved in the algorithm, its time complexity, and its practical applications. Additionally, it includes self-assessment questions and references for further learning.

Uploaded by

sivaprasadsoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Shell Sort 21

The document provides an overview of Shell Sort, a sorting algorithm that improves upon insertion sort by allowing the exchange of far items, thus reducing the number of swaps and comparisons. It outlines the steps involved in the algorithm, its time complexity, and its practical applications. Additionally, it includes self-assessment questions and references for further learning.

Uploaded by

sivaprasadsoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of BES-1

DATA STRUCTURES
24SC1203
Topic:

SHELL SORT

Session - 1

CREATED BY K. VICTOR BABU


AIM OF THE SESSION

To familiarize students with the basic concept of Shell Sort

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1. Demonstrate
2. Describe the working of shell sort technique
3. List out the steps of the shell sort algorithm

LEARNING OUTCOMES

At the end of this session, you should be able to:


1. Define Shell Sort
2. Describe an algorithm and time complexity of Shell Sort
3. Summarize the Shell Sort

CREATED BY K. VICTOR BABU


SESSION INTRODUCTION

• Shell sort is a generalized version of the insertion sort algorithm.


• Shell sort invented by Donald Shell.
• That is why name as shell sort
• Shell sort reduces the number of swaps and comparisons
required, making it more efficient than insertion sort
• It first sorts elements that are far apart from each other and
successively reduces the interval between the elements to be
sorted.
• Shell sort implemented by using “Gap” sequence(N/2,N/4….1)
• Gap is a mathematical formula gap=N/2
• Here N is number of elements in the list
CREATED BY K. VICTOR BABU
SESSION DESCRIPTION

Steps Involved in Shell sort-


Step 1: Set the gap value N/2 to j and first index set to i
Step 2: Compare i with j which is at the gap N/2
Step 3: increment i and j values
Step 4: Repeat step-2 until j reaches to end of the list up to this si
pass-1.
Step 5: In pass-2 the gap value is gap=gap/2
Step 6: this procedure will continue until gap reaches to 1
Step7: when gap=1 shell sort work as insertion sort
Step8: Using insertion sort the entire list would be in sorting order

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

Working of Shell Sort


Suppose, we need to sort the following array

• We are using the shell's original sequence (N/2, N/4, ...1) as intervals in our algorithm.
• In the first loop, if the array size is N = 8 then, the elements lying at the interval of N/2 =
4 are compared and swapped if they are not in order.
• The 0th element is compared with the 4th element.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

If the 0th element is greater than the 4th one then, the 4th element is first stored in temp
variable and the 0th element (ie. greater element) is stored in the 4th position and the element
stored in temp is stored in the 0th position.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

This process goes on for all the remaining elements.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

In the second loop, an interval of N/4 = 8/4 = 2 is taken and again the elements lying at these intervals are
sorted

• The elements at 4th and 2nd position are compared. The elements at 2nd and 0th position are also
compared. All the elements in the array lying at the current interval are compared.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

The same process goes on for remaining elements

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

Finally, when the interval


is N/8 = 8/8 =1 then the
array elements lying at
the interval of 1 are
sorted. The array is now
completely sorted.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

Shell Sort Algorithm Pseudocode

Procedure begin: Shell sort


Calculate gap size ($gap)
WHILE $gap is greater than 0
FOR each element of the list, that is $gap apart
Extract the current item
Locate the position to insert
Insert the item to the position
END FOR
Calculate gap size ($gap)
END WHILE
End procedure

Note: Initially the $gap is half of length of the array and the $gap size is recalculated by dividing the gap by 2 every time

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

Complexity of Shell Sort Algorithm

Complexity in the Best Case: O(n*Log n)

Complexity in the Average Case: O(n*log n)

Complexity in the Worst-Case Scenario: O (n2)

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

•Since Shell sort can be implemented using little code and does
not use the call stack, some implementations of the qsort
function in the C standard library targeted at embedded
systems use it instead of quicksort. Shell sort is, for example,
used in the uClibc library. For similar reasons, an
implementation of Shell sort is present in the Linux kernel.

•Shell sort can also serve as a sub-algorithm of introspective


sort, to sort short subarrays and to prevent a slowdown when
the recursion depth exceeds a given limit. This principle is
employed, for instance, in the bzip2 compressor.

CREATED BY K. VICTOR BABU


EXAMPLES

CREATED BY K. VICTOR BABU


SUMMARY

Shell Sort is mainly a variation of Insertion Sort. In insertion sort,


we move elements only one position ahead. When an element
has to be moved far ahead, many movements are involved. The
idea of Shell Sort is to allow the exchange of far items. In Shell
sort, we make the array h-sorted for a large value of h. We keep
reducing the value of h until it becomes 1. An array is said to be
h-sorted if all sublists of every hth element are sorted.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. What is the other name for a shell sort algorithm?

(a) Diminishing increment sort


(b) Diminishing decrement sort
(c) Insertion Sort
(d) Selection Sort

2. The worst case running time of shell sort, using Shell’s increments is?

(a) O(N)
(b) O(N log N)
(c) O(log N)
(d) O(N2)
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS

1. Describe what is Shell sort

2. List out steps involved in Shell sort

3. Analyze complexity of Shell sort

4. Summarize the working principle of Shell sort

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:
1. . Data Structure Through C by Yashavant P. Kanetkar
2. Algorithms in a Nutshell by George T. Heineman, Gary Pollice and Stanley
Selkow

Sites and Web links:


1. https://www.javatpoint.com/shell-sort

CREATED BY K. VICTOR BABU


THANK YOU

Team – Data Structure

CREATED BY K. VICTOR BABU

You might also like