Shell Sort 21
Shell Sort 21
DATA STRUCTURES
24SC1203
Topic:
SHELL SORT
Session - 1
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
• 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.
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.
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.
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
•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.
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
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