SlideShare a Scribd company logo
SHELL SORTSHELL SORT
Generalization ofGeneralization of
insertioninsertion
Introduction
Shell sort is one of the oldest sorting
algorithm devised by Donald Shell in
1959.
Shell sort is also called the
diminishing increment sort.
The shell sort was introduced to
improve the efficiency of simple
insertion sort .Group 4 02/07/18 05:22 AM
In insertion sort, we move elements only
one position ahead. When an element has
to be moved far ahead, many movements
are involved.
The shell sort improved the insertion sort
by comparing elements far apart, then
elements less far apart and finally
comparing adjacent elements.
The shell sort algorithm avoids large shifts
as in the case of insertion sort, if the
smaller values is to the far right and has to
be moved to the far left.
Group 4 02/07/18 05:22 AM
The shell sort improved insertion sort by
breaking the original list into a number of
smaller sublist, each of which is sorted
using an insertion sort.
The unique way these sublists are chosen is
through the key to the shell sort.
Instead of breaking the list into sublists of
contiguous items, the shell sort uses an
incremental i, sometimes called the gap or
interval to create a sublist by choosing all
items that i items apart.
Group 4 02/07/18 05:22 AM
The idea of shell Sort is to allow exchange
of far items
Group 4 02/07/18 05:22 AM
key terms
Gap or interval: the spacing between elements
or the distance between items being
sorted. As we progress, the gap decreases
and that is why Shell Sort is also called
Diminishing Gap Sort.
Swap: exchanging one thing for another.
(interchange, exchange, switch). When an
element is swapped, the move the position of
the swapped item.
Sort: the arrangement of data in a prescribed
sequence.
Group 4 02/07/18 05:22 AM
Integer (INT): a whole number; a number
that is not a fraction.
Sub-list: a list gotten from a larger list. For
instance, in sets a sub-set is gotten from the
universal set.
Shuttle sort: A sorting method based on
swaps of pairs of numbers. Swapping may be
done or no swapping may be required. The
sort works from left to right, reconsidering
earlier pairs when a swap is made.
Group 4 02/07/18 05:22 AM
How Shell Sort Works
steps
Group 4 02/07/18 05:22 AM
There are many ways of choosing the
next gap.
In any item, objects are compared with less
than (<), equal (=) or greater then (>) in
cases where strings may be applied.
Group 4 02/07/18 05:22 AM
1. Count the number of elements in
a given array. E.g an array
containing
35, 33,42,10,14,19,27,44
This array has how many
elements?
Ans: 8Group 4 02/07/18 05:22 AM
Group 4 02/07/18 05:22 AM
We make a virtual sub-list of all values
located at the interval of 4 position.
The values for the 4 sub-list for the first
gap.
{35, 14}, {33, 19}, {42, 27} and {10, 44}
How comes
Group 4 02/07/18 05:22 AM
{35, 14}if 35>14 then we
swap in there position
otherwise is left.
Next {33, 19} if 33>19 then
swap to their position.
Next {42, 27} if 42>27 then
swap.
Next {10, 44} if 10> 44 swap
otherwise remain in the same
position.
We compare values in each
sub-list and swap them (if
necessary) in the original
array.
The new
array after
pass 1: Group 402/07/18 05:22 AM
Group 402/07/18 05:22 AM
Example 1
Consider the following element in an array
A: 54,26,93,17,77,31,44,55,20
Using shell sort, let us sort the array using
gap of 3.
Sub-list 1{54,17,44)
Sub-list 2 {26,77,55)
Sub-list 3 {93,31,20)
Then compare the sub-lists and swap
We shall have
02/07/18 05:22 AMGroup 4
Group 4 02/07/18 05:22 AM
Group 402/07/18 05:22 AM
Example 2
Consider the following unsorted
array A with elements
80, 93, 60, 12, 42, 30, 68, 85, 10
Sort using the Shell sort.
Group 4 02/07/18 05:22 AM
No of comparison: 6
No of swaps: 4
Group 402/07/18 05:22 AM
Group 402/07/18 05:22 AM
Group 402/07/18 05:22 AM
Example 3
Consider an array T with
the following elements
8, 2, 5, 4, 7, 1, 3, 6,
Sort using Shell sort.
Group 4 02/07/18 05:22 AM
Total numbers of element 8. InitiallyGap = 4
Number of comparison: 4
Number of swap: 3
Group 402/07/18 05:22 AM
3 1 5 2 8 67 4
7 1 3 54 68 2
Group 402/07/18 05:22 AM
Group 402/07/18 05:22 AM
Example 4
sorting elements that are far apart
Consider an array with elements
40, 2, 1, 43, 3, 65, 0, -1, 58, 3,42, 4
Using initial gap as 5
Group 4 02/07/18 05:22 AM
40
2 1 43 3 65 0 -1 58 3 42 4
40 0 -1 43 3 42 2 1 58 3 65 4
2 0 -1 3 1 4 40 3 42 43 65 58
-1
0 1 2 3 3 4 40 42 43 58 65
Original:
After 5-sort:
After 3-sort:
After 1-sort:
Group 402/07/18 05:22 AM
Before and after sorting with gap = 7
Before and after sorting with gap = 3
Example 5
Group 402/07/18 05:22 AM
Class work
Q1.
Given the following list of numbers:
[5, 16, 20, 12, 3, 8, 9, 17, 19, 7]
Which answer illustrates the contents
of the list after all swapping is
complete for a gap size of 3?
02/07/18 05:22 AM
02/07/18 05:22 AMGroup 4
(A) [5, 3, 8, 7, 16, 19, 9, 17, 20, 12]
• (Correct! Each group of numbers represented by index
positions 3 apart are sorted correctly)
(B) [3, 7, 5, 8, 9, 12, 19, 16, 20, 17]
• (Incorrect. This solution is for a gap size of two.)
(C) [3, 5, 7, 8, 9, 12, 16, 17, 19, 20]
• (Incorrect. This is list completely sorted, you have gone
too far.)
(D) [5, 16, 20, 3, 8, 12, 9, 17, 20, 7]
(Incorrect. The gap size of three indicates that the group
represented by every third number e.g. 0, 3, 6, 9 and 1, 4, 7
and 2, 5, 8 are sorted not groups of 3.)
Best case
The best case is when the array is already
sorted in the right order.
The number of comparisons is almost zero and
swapping.
Average case
Less comparison and swamping
Worst case
More comparison and swapping
Group 4 02/07/18 05:22 AM
Advantages
1. Shell sort is easy to implement but not
easier than insertion sort.
2. Shell sort can be sorted for a gap greater
than one and thus less exchange than
insertion sort hence, fast.
3. Efficient for medium size lists.
4. It is easy to understand.
5. It is easy to implement.
Group 4 02/07/18 05:22 AM
Disadvantages
1. It is a complex algorithm and its not nearly
as efficient as the merge, heap and quick
sorts.
2.
Group 4 02/07/18 05:22 AM
Algorithm
The difference between insertion sort from
the shell sort are
– There is one additional loop (the outer loop)-
each run processes one increment.
– The middle and the most inner loops are same
as in the insertion sort with gap = 1.
Group 4 02/07/18 05:22 AM
Group 4
Let A be a linear array of n elements, A [1], A [2], A
[3], ...... A[ n ] and Incr be an array of sequence of
span to be incremented in each pass. X is the number
of elements in the array Incr. Span is to store the span
of the array from the array Incr.
1. Input n numbers of an array A
2. Initialise i = 0 and repeat through step 6 if ( i < x )
3. Span = Incr[ i ]
4. Initialise j = span and repeat through step 6 if ( j < n )
( a ) Temp = A[ j ]
5. Initialise k = j-span and repeat through step 5 if ( k > = 0) and
(temp < A [ k ])
( a ) A [ k + span] = A [ k ]
6. A [ k + span] = temp
7. Exit
02/07/18 05:22 AM Group 4
Summary
02/07/18 05:22 AM Group 4
02/07/18 05:22 AM Group 4
Shell Sort Algorithm
1. Set gap to n/2
2. while gap > 0
3. for each element from gap to end, by
gap
4. Insert element in its gap-separated sub-
array
5. if gap is 2, set it to 1
6. otherwise set it to gap / 2.2
Group 4 02/07/18 05:22 AM
Shell Sort Algorithm: Inner
Loop
set nextPos to position of element to insert
3.2 set nextVal to value of that element
3.3 while nextPos > gap and
element at nextPos-gap is > nextVal
3.4 Shift element at nextPos-gap to
nextPos
3.5 Decrement nextPos by gap
3.6 Insert nextVal at nextPos
Group 4 02/07/18 05:22 AM
# Sort an array a[0...n-1].
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
# Start with the largest gap and work down to a gap of 1
For each (gap in gaps)
{
# Do a gapped insertion sort for this gap size.
# The first gap elements a[0..gap-1] are already in gapped order
# keep adding one more element until the entire array is gap sorted
for (i = gap; i < n; i += 1)
{
# add a[i] to the elements that have been gap sorted
# save a[i] in temp and make a hole at position i
temp = a[i]
# shift earlier gap-sorted elements up until the correct location for a[i] is found
for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
{
a[j] = a[j - gap]
}
# put temp (the original a[i]) in its correct location
a[j] = temp
}
}
Group 402/07/18 05:22 AM
int j, p, gap;
comparable tmp;
for (gap = N/2; gap > 0; gap = gap/2)
for ( p = gap; p < N ; p++)
{
tmp = a[p];
for (j = p; j >= gap && tmp < a[j- gap]; j = j - gap)
a[j] = a[j-gap];
a[j] = tmp;
}
}
Group 4 02/07/18 05:22 AM
Ad

More Related Content

What's hot (20)

Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Shell sort
Shell sortShell sort
Shell sort
Rajendran
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Rashmi R Upadhya
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
geeortiz
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
Ashish Gaurkhede
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Shell sort
Shell sortShell sort
Shell sort
Nauman Ali
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
Himanshu Kesharwani
 
Quick sort
Quick sortQuick sort
Quick sort
Afaq Mansoor Khan
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
samairaakram
 
Selection sort
Selection sortSelection sort
Selection sort
stella D
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
Meherul1234
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
V.V.Vanniaperumal College for Women
 
Hash table
Hash tableHash table
Hash table
Rajendran
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 

Similar to Shell sorting (20)

Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
SHELL SORT-2.pptx
SHELL SORT-2.pptxSHELL SORT-2.pptx
SHELL SORT-2.pptx
CS50Bootcamp
 
Design and analysis of algorithm final course
Design and analysis of algorithm final courseDesign and analysis of algorithm final course
Design and analysis of algorithm final course
Abdul salam
 
Design and analysis of algorithm final course
Design and analysis of algorithm final courseDesign and analysis of algorithm final course
Design and analysis of algorithm final course
Abdul salam
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
sheraz7288
 
Arrays
ArraysArrays
Arrays
shillpi29
 
Various Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptxVarious Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptx
atirathpal007
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
DrRanjeetKumar51721
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
Krish_ver2
 
enhancement of sorting algorithm
enhancement of sorting algorithmenhancement of sorting algorithm
enhancement of sorting algorithm
Rana assad ali
 
Sorting
SortingSorting
Sorting
Govind Upadhyay
 
Sorting
SortingSorting
Sorting
Abhishek Khune
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Sorting
SortingSorting
Sorting
Ghaffar Khan
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Design and analysis of algorithm final course
Design and analysis of algorithm final courseDesign and analysis of algorithm final course
Design and analysis of algorithm final course
Abdul salam
 
Design and analysis of algorithm final course
Design and analysis of algorithm final courseDesign and analysis of algorithm final course
Design and analysis of algorithm final course
Abdul salam
 
Various Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptxVarious Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptx
atirathpal007
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
Krish_ver2
 
enhancement of sorting algorithm
enhancement of sorting algorithmenhancement of sorting algorithm
enhancement of sorting algorithm
Rana assad ali
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ad

More from TUC (12)

Project management personnel in Computing
Project management personnel in Computing Project management personnel in Computing
Project management personnel in Computing
TUC
 
Child abuse and protection in schools
Child abuse and protection in schoolsChild abuse and protection in schools
Child abuse and protection in schools
TUC
 
Computer network and interent
Computer network and interentComputer network and interent
Computer network and interent
TUC
 
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
TUC
 
Timeline of nazi
Timeline of naziTimeline of nazi
Timeline of nazi
TUC
 
Social teachings of the church
Social teachings of the churchSocial teachings of the church
Social teachings of the church
TUC
 
Social teachings of the church
Social teachings of the churchSocial teachings of the church
Social teachings of the church
TUC
 
Evalution of educational planning
Evalution of educational planningEvalution of educational planning
Evalution of educational planning
TUC
 
History of holocaust
History of holocaustHistory of holocaust
History of holocaust
TUC
 
The holocaulst
The holocaulstThe holocaulst
The holocaulst
TUC
 
Social teachings of the church pre
Social teachings of the church preSocial teachings of the church pre
Social teachings of the church pre
TUC
 
Relevance of charity in truth
Relevance of charity in truthRelevance of charity in truth
Relevance of charity in truth
TUC
 
Project management personnel in Computing
Project management personnel in Computing Project management personnel in Computing
Project management personnel in Computing
TUC
 
Child abuse and protection in schools
Child abuse and protection in schoolsChild abuse and protection in schools
Child abuse and protection in schools
TUC
 
Computer network and interent
Computer network and interentComputer network and interent
Computer network and interent
TUC
 
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
Transcendental meditation-a-useful-tool-in-teaching-and-learning-in-the 2
TUC
 
Timeline of nazi
Timeline of naziTimeline of nazi
Timeline of nazi
TUC
 
Social teachings of the church
Social teachings of the churchSocial teachings of the church
Social teachings of the church
TUC
 
Social teachings of the church
Social teachings of the churchSocial teachings of the church
Social teachings of the church
TUC
 
Evalution of educational planning
Evalution of educational planningEvalution of educational planning
Evalution of educational planning
TUC
 
History of holocaust
History of holocaustHistory of holocaust
History of holocaust
TUC
 
The holocaulst
The holocaulstThe holocaulst
The holocaulst
TUC
 
Social teachings of the church pre
Social teachings of the church preSocial teachings of the church pre
Social teachings of the church pre
TUC
 
Relevance of charity in truth
Relevance of charity in truthRelevance of charity in truth
Relevance of charity in truth
TUC
 
Ad

Recently uploaded (20)

How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 

Shell sorting

  • 1. SHELL SORTSHELL SORT Generalization ofGeneralization of insertioninsertion
  • 2. Introduction Shell sort is one of the oldest sorting algorithm devised by Donald Shell in 1959. Shell sort is also called the diminishing increment sort. The shell sort was introduced to improve the efficiency of simple insertion sort .Group 4 02/07/18 05:22 AM
  • 3. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The shell sort improved the insertion sort by comparing elements far apart, then elements less far apart and finally comparing adjacent elements. The shell sort algorithm avoids large shifts as in the case of insertion sort, if the smaller values is to the far right and has to be moved to the far left. Group 4 02/07/18 05:22 AM
  • 4. The shell sort improved insertion sort by breaking the original list into a number of smaller sublist, each of which is sorted using an insertion sort. The unique way these sublists are chosen is through the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an incremental i, sometimes called the gap or interval to create a sublist by choosing all items that i items apart. Group 4 02/07/18 05:22 AM
  • 5. The idea of shell Sort is to allow exchange of far items Group 4 02/07/18 05:22 AM
  • 6. key terms Gap or interval: the spacing between elements or the distance between items being sorted. As we progress, the gap decreases and that is why Shell Sort is also called Diminishing Gap Sort. Swap: exchanging one thing for another. (interchange, exchange, switch). When an element is swapped, the move the position of the swapped item. Sort: the arrangement of data in a prescribed sequence. Group 4 02/07/18 05:22 AM
  • 7. Integer (INT): a whole number; a number that is not a fraction. Sub-list: a list gotten from a larger list. For instance, in sets a sub-set is gotten from the universal set. Shuttle sort: A sorting method based on swaps of pairs of numbers. Swapping may be done or no swapping may be required. The sort works from left to right, reconsidering earlier pairs when a swap is made. Group 4 02/07/18 05:22 AM
  • 8. How Shell Sort Works steps Group 4 02/07/18 05:22 AM
  • 9. There are many ways of choosing the next gap. In any item, objects are compared with less than (<), equal (=) or greater then (>) in cases where strings may be applied. Group 4 02/07/18 05:22 AM
  • 10. 1. Count the number of elements in a given array. E.g an array containing 35, 33,42,10,14,19,27,44 This array has how many elements? Ans: 8Group 4 02/07/18 05:22 AM
  • 11. Group 4 02/07/18 05:22 AM
  • 12. We make a virtual sub-list of all values located at the interval of 4 position. The values for the 4 sub-list for the first gap. {35, 14}, {33, 19}, {42, 27} and {10, 44} How comes Group 4 02/07/18 05:22 AM
  • 13. {35, 14}if 35>14 then we swap in there position otherwise is left. Next {33, 19} if 33>19 then swap to their position. Next {42, 27} if 42>27 then swap. Next {10, 44} if 10> 44 swap otherwise remain in the same position. We compare values in each sub-list and swap them (if necessary) in the original array. The new array after pass 1: Group 402/07/18 05:22 AM
  • 15. Example 1 Consider the following element in an array A: 54,26,93,17,77,31,44,55,20 Using shell sort, let us sort the array using gap of 3. Sub-list 1{54,17,44) Sub-list 2 {26,77,55) Sub-list 3 {93,31,20) Then compare the sub-lists and swap We shall have 02/07/18 05:22 AMGroup 4
  • 16. Group 4 02/07/18 05:22 AM
  • 18. Example 2 Consider the following unsorted array A with elements 80, 93, 60, 12, 42, 30, 68, 85, 10 Sort using the Shell sort. Group 4 02/07/18 05:22 AM
  • 19. No of comparison: 6 No of swaps: 4 Group 402/07/18 05:22 AM
  • 22. Example 3 Consider an array T with the following elements 8, 2, 5, 4, 7, 1, 3, 6, Sort using Shell sort. Group 4 02/07/18 05:22 AM
  • 23. Total numbers of element 8. InitiallyGap = 4 Number of comparison: 4 Number of swap: 3 Group 402/07/18 05:22 AM
  • 24. 3 1 5 2 8 67 4 7 1 3 54 68 2 Group 402/07/18 05:22 AM
  • 26. Example 4 sorting elements that are far apart Consider an array with elements 40, 2, 1, 43, 3, 65, 0, -1, 58, 3,42, 4 Using initial gap as 5 Group 4 02/07/18 05:22 AM
  • 27. 40 2 1 43 3 65 0 -1 58 3 42 4 40 0 -1 43 3 42 2 1 58 3 65 4 2 0 -1 3 1 4 40 3 42 43 65 58 -1 0 1 2 3 3 4 40 42 43 58 65 Original: After 5-sort: After 3-sort: After 1-sort: Group 402/07/18 05:22 AM
  • 28. Before and after sorting with gap = 7 Before and after sorting with gap = 3 Example 5 Group 402/07/18 05:22 AM
  • 29. Class work Q1. Given the following list of numbers: [5, 16, 20, 12, 3, 8, 9, 17, 19, 7] Which answer illustrates the contents of the list after all swapping is complete for a gap size of 3? 02/07/18 05:22 AM
  • 30. 02/07/18 05:22 AMGroup 4 (A) [5, 3, 8, 7, 16, 19, 9, 17, 20, 12] • (Correct! Each group of numbers represented by index positions 3 apart are sorted correctly) (B) [3, 7, 5, 8, 9, 12, 19, 16, 20, 17] • (Incorrect. This solution is for a gap size of two.) (C) [3, 5, 7, 8, 9, 12, 16, 17, 19, 20] • (Incorrect. This is list completely sorted, you have gone too far.) (D) [5, 16, 20, 3, 8, 12, 9, 17, 20, 7] (Incorrect. The gap size of three indicates that the group represented by every third number e.g. 0, 3, 6, 9 and 1, 4, 7 and 2, 5, 8 are sorted not groups of 3.)
  • 31. Best case The best case is when the array is already sorted in the right order. The number of comparisons is almost zero and swapping. Average case Less comparison and swamping Worst case More comparison and swapping Group 4 02/07/18 05:22 AM
  • 32. Advantages 1. Shell sort is easy to implement but not easier than insertion sort. 2. Shell sort can be sorted for a gap greater than one and thus less exchange than insertion sort hence, fast. 3. Efficient for medium size lists. 4. It is easy to understand. 5. It is easy to implement. Group 4 02/07/18 05:22 AM
  • 33. Disadvantages 1. It is a complex algorithm and its not nearly as efficient as the merge, heap and quick sorts. 2. Group 4 02/07/18 05:22 AM
  • 34. Algorithm The difference between insertion sort from the shell sort are – There is one additional loop (the outer loop)- each run processes one increment. – The middle and the most inner loops are same as in the insertion sort with gap = 1. Group 4 02/07/18 05:22 AM
  • 35. Group 4 Let A be a linear array of n elements, A [1], A [2], A [3], ...... A[ n ] and Incr be an array of sequence of span to be incremented in each pass. X is the number of elements in the array Incr. Span is to store the span of the array from the array Incr. 1. Input n numbers of an array A 2. Initialise i = 0 and repeat through step 6 if ( i < x ) 3. Span = Incr[ i ] 4. Initialise j = span and repeat through step 6 if ( j < n ) ( a ) Temp = A[ j ] 5. Initialise k = j-span and repeat through step 5 if ( k > = 0) and (temp < A [ k ]) ( a ) A [ k + span] = A [ k ] 6. A [ k + span] = temp 7. Exit
  • 36. 02/07/18 05:22 AM Group 4 Summary
  • 37. 02/07/18 05:22 AM Group 4
  • 38. 02/07/18 05:22 AM Group 4
  • 39. Shell Sort Algorithm 1. Set gap to n/2 2. while gap > 0 3. for each element from gap to end, by gap 4. Insert element in its gap-separated sub- array 5. if gap is 2, set it to 1 6. otherwise set it to gap / 2.2 Group 4 02/07/18 05:22 AM
  • 40. Shell Sort Algorithm: Inner Loop set nextPos to position of element to insert 3.2 set nextVal to value of that element 3.3 while nextPos > gap and element at nextPos-gap is > nextVal 3.4 Shift element at nextPos-gap to nextPos 3.5 Decrement nextPos by gap 3.6 Insert nextVal at nextPos Group 4 02/07/18 05:22 AM
  • 41. # Sort an array a[0...n-1]. gaps = [701, 301, 132, 57, 23, 10, 4, 1] # Start with the largest gap and work down to a gap of 1 For each (gap in gaps) { # Do a gapped insertion sort for this gap size. # The first gap elements a[0..gap-1] are already in gapped order # keep adding one more element until the entire array is gap sorted for (i = gap; i < n; i += 1) { # add a[i] to the elements that have been gap sorted # save a[i] in temp and make a hole at position i temp = a[i] # shift earlier gap-sorted elements up until the correct location for a[i] is found for (j = i; j >= gap and a[j - gap] > temp; j -= gap) { a[j] = a[j - gap] } # put temp (the original a[i]) in its correct location a[j] = temp } } Group 402/07/18 05:22 AM
  • 42. int j, p, gap; comparable tmp; for (gap = N/2; gap > 0; gap = gap/2) for ( p = gap; p < N ; p++) { tmp = a[p]; for (j = p; j >= gap && tmp < a[j- gap]; j = j - gap) a[j] = a[j-gap]; a[j] = tmp; } } Group 4 02/07/18 05:22 AM