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)

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
 
Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
Niyati Thaker
 
Shellshort ppt
Shellshort pptShellshort ppt
Shellshort ppt
hannatamayao
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Sorting And Type of Sorting
Sorting And Type of SortingSorting And Type of Sorting
Sorting And Type of Sorting
MITULJAMANG
 
Sorting
SortingSorting
Sorting
Gopi Saiteja
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Rashmi R Upadhya
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Shell sort
Shell sortShell sort
Shell sort
Rajendran
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Selection sort
Selection sortSelection sort
Selection sort
smlagustin
 
The selection sort algorithm
The selection sort algorithmThe selection sort algorithm
The selection sort algorithm
David Burks-Courses
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
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
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Sorting And Type of Sorting
Sorting And Type of SortingSorting And Type of Sorting
Sorting And Type of Sorting
MITULJAMANG
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 

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)

Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 

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
  翻译: