You are on page 1of 11

SHELL SORT

ALGORITHMS
Name: Jude Edwards
Course Name: Data Structures & Analysis of Algorithms
Course Code: 2100
Lecturer: Jason Phillips
TABLE OF CONTENTS

01 02 03
CODING /
ALGORIT IMPLEMENTATIO
THEORY HM N
THEORY 01
SHELL
ALGORITHMS

Shell sort is mainly a variation of Insertion Sort. In insertion sort, we move elements
only one position ahead. When an element must 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 gap-sorted for a large value of ‘gap’. We keep
reducing the value of ‘gap’ until it becomes 1.

ARR [9] = {80, 156, 90, 568, 25, 62, 8, 4, 185}


SHELL ALGORITHMS
(POINTS)
● Shell algorithms are more beneficial in usage cases whereby Array sizes are large
where smaller elements are stationed towards the very end.

● Efficiency is determined on how the GAP / Interval is set.

● Can be used when calling a stack overhead or when a recursion exceeds its limit.
ALGORIT
HM 02
SHELL ALGORITHMS (How does it work )

Narrative Representation Pseudocode


PROCEDURE SHELL_SORT(ARRAY, N)
1. Accept an array and its size (n). WHILE GAP < LENGTH(ARRAY) /3 :
GAP = ( INTERVAL * 3 ) + 1
2. Initialize the value of the interval / gap. END WHILE LOOP
WHILE GAP > 0 :
FOR (OUTER = GAP; OUTER < LENGTH(ARRAY); OUTER++):
3. Compare elements at the distance of gap for INSERTION_VALUE = ARRAY[OUTER]
INNER = OUTER;
each iteration. WHILE INNER > GAP-1 AND ARRAY[INNER – GAP] >=
INSERTION_VALUE:
ARRAY[INNER] = ARRAY[INNER – GAP]
4. If the element at the left is larger than the INNER = INNER – GAP
END WHILE LOOP
element at the right, sort. ARRAY[INNER] = INSERTION_VALUE
END FOR LOOP
GAP = (GAP -1) /3;
5. Continue until the array is sorted. END WHILE LOOP
END SHELL_SORT
SHELL ALGORITHMS ( How does it work )

ARR [9] = {80, 156, 90, 568, 25, 62, 8, 4, 185} GAP = 9 / 2 = 4.5 4

ARR [9] = {25, 156, 90, 568, 80, 62, 8, 4, 185}

ARR [9] = {25, 62, 90, 568, 80, 156, 8, 4, 185}

ARR [9] = {25, 62, 8, 568, 80, 156, 90, 4, 185}

ARR [9] = {25, 62, 8, 4, 80, 156, 90, 568, 185}

FIRST ITERATION
SHELL ALGORITHMS ( How does it work )

ARR [9] = {25, 62, 8, 4, 80, 156, 90, 568, 185} GAP = 4/2 = 2
ARR [9] = {8,
62, 25, 4, 80, 156, 90, 568, 185}

ARR [9] = {8, 4, 25, 62, 80, 156, 90, 568, 185}

ARR [9] = {8, 62, 25, 4, 80, 156, 90, 568, 185}

SECOND ITERATION
SHELL ALGORITHMS ( How does it work )

ARR [9] = {8, 62, 25, 4, 80, 156, 90, 568, 185} GAP = 2/2 = 1
ARR [9] = {8,
25, 62, 4, 80, 156, 90, 568, 185}

ARR [9] = {8, 25, 4, 62, 80, 156, 90, 568, 185}

ARR [9] = {8, 4, 25, 62, 80, 156, 90, 568, 185}

ARR [9] = {4, 8, 25, 62, 80, 90, 156, 568, 185}

ARR [9] = {4, 8, 25, 62, 80, 90, 156, 185, 568} THIRD ITERATION
CODING &
IMPLEMENTAT
ION 03

You might also like