SlideShare uma empresa Scribd logo
1 de 27
AKASH GUPTA
LIKHITH
AJEETH KUMAR
P VIKRAM
ANUPA ROHIT
BHUSHAN
B NANDU
A MAHESH
IDREES SHEIK
DENNIS
SORT
TYPES OF SORT
HISTORY AND USAGE
ALGORITHM
PRINCIPLES OF WORKING
COMPLEXITY
ADVANTAGES AND DRAWBACKS
APPLICATIONS
CONCLUSION
OUTLINE
SORT
BEFORE WE START WITH SHELL SORT, LET US FIRST KNOW ABOUT SORTING
 What is sorting?
 Sorting is the process of arranging elements either in ascending (or) descending order. The term
sorting came into existence when humans realized the importance of searching quickly.
 Why do we use sorting?
 A sorting algorithm will put items in a list into an order, such as alphabetical or numerical order. For
example, a list of customer names could be sorted into alphabetical order by surname, or a list of
people could be put into numerical order by age.
Bubble
sort
Selection
Sort
Quick
Sort
Merge
Sort
Heap
Sort
Insertion
Sort
Shell
Sort
THE SORT WE CHOSE TO EXPLAIN IS THE SHELL SORT:
 BASICALLY IF WE ASK ANYONE OVER HERE ABOUT THE
TYPES OF SORTS, THEY SAY THE TYPES WE PREVIOUSLY
LEARNT. BUT THE SHELL SORT IS THE TYPE WHERE WE
NEVER HEARD OR LEARNT IN OUR EARLIER STAGES OF
EDUCATION.
 So what exactly is shell sort?
 Shell sort is the generalization of the insertion sort. It first sorts
elements that are far apart from each other and then reduces the
gap between the elements to be sorted. Gap reduction is based on
the sequence used. It is basically faster than the other sorts. We
use this sort to fasten our process.
HISTORY AND THE BASICS OF SHELL SORT
The shell sort (also known as shell's method) is named after its inventor, Donald Shell, who
published the algorithm in 1959. This was invented at university of Cincinnati.
Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The
increment size is reduced after each pass until the increment size is 1.
With an increment size of 1, the sort is a basic insertion sort, but by this time the data is
guaranteed to be almost sorted, which is insertion sort's "best case".
Any sequence will sort the data as long as it ends in 1, but some sequences work better than
others.
WHY DO WE USE SHELL
SORT
The shell sort is used to sort the array by sorting the pair of elements far apart from each other
depending on it's gap/interval and reduces the gap between the elements to be sorted. It can transfer the
disordered elements into the correct place faster than any other sort.
Shell sort has improved the average time complexity of insertion sort. As similar to insertion sort, it is a
comparison-based and in-place sorting algorithm. Shell sort is efficient for medium-sized data sets.
This algorithm first sorts the elements that are far away from each other, then it subsequently reduces
the gap between them. This gap is called as interval. This interval can be calculated by using
the Knuth's formula given below -
HH = H * 3 + 1 (where, 'h' is the interval having initial value 1)
THE ALGORITHM & HOW DOES IT WORK?
 Let's see the working of the shell sort algorithm. To understand the working of the shell sort algorithm, let’s take an
unsorted array. It will be easier to understand the shell sort via an example.
• Let the elements of array be –
23 29 15 19 31 7 9 5 2
 We will use the original sequence of shell sort, i.e. N/2, n/4,....,1 as the intervals.
 Step-1: Insert all the elements in array a[20]
 Step-2: In 1st pass, Gap= ⌊N/2⌋ = ⌊9/2⌋ = ⌊4.5⌋ = 4
 So the elements of the gap/interval of 4 are compared and swapped if they are out of order in the first gap in both
forward & backward directions.
 Here, 0th element and the 4th element are compared.
23 29 15 19 31 7 9 5 2
0 1 2 3 4 5 6 7 8
INDEX ->
ELEMENTS->
 If the 0th element > 4th element then both are swapped.
 If the 0th element == 4th element then no swapping is required.
 The rearranged elements in an n/2 interval:
Array: [ 23 29 15 19 31 7 9 5 2 ]
 Similarly, this procedure is repeated for all remaining elements & rearranges the elements in an n/2 interval, i.e
1st 5th
2nd 6th
3rd 7th
4th 0th
23 29 15 19 31 7 9 5 2
23 7 15 19 31 29 9 5 2
23 7 9 19 31 29 15 5 2
23 7 9 5 31 29 15 19 2
23 7 9 5 2 29 15 19 31
2 7 9 5 23 29 15 19 31
PASS-1
RESULT OF PASS-1:
 Step-3: In 2nd pass, gap = ⌊N/4⌋ = ⌊9/4⌋ = ⌊2.5⌋ = 2 [2 is the chosen interval]
 Here, the elements are sorted & stored which are at the gap of 2.
 Until J < = N – 1, run
2 7 9 5 23 29 15 19 31
2 5 9 7 23 29 15 19 31
2 5 9 7 15 29 23 19 31
2 5 9 7 15 19 23 29 31
 Compare 0th element with 2nd element and go on….
0 1 2 3 4 5 6 7 8
2 7 9 5 23 29 15 19 31
In N/4 Interval
Result Of Pass-2
INDEX ->
ELEMENTS->
 Step-4: In 3rd pass
 Here, shell sort works same as insertion sort
 Gap = ⌊N/8⌋ = ⌊9/8⌋ = ⌊1.11⌋ = 1
2 5 9 7 15 19 23 29 31
 Step-5: Gap = ⌊n/16⌋ = 0
 We can see the sorted array of elements.
2 5 9 7 15 19 23 29 31
FLOWCHART OF SHELL SORT
CODE SNIPPET OF SHELL SORT:
int main()
{
int a[20],i,n;
printf("Enter number of
elements:");
scanf("%d", &n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
sort(a, n);
printf("n Array after shell sort:n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
#include<stdio.h>
void sort(int a[],int n)
{
int gap, i, j, temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i = gap; i < n; i+=1)
{
temp=a[i];
for(j=i; j>=gap&&a[j-gap]>temp;
j-=gap)a[j]=a[j-gap];a[j]=temp;
}
}
}
Output:
Enter number of elements:
5
Enter array elements: 5 6 7
2 9 12
Array after shell sort: 2 7 9
12 56
THE PRINCIPLES OF ITS WORKING:
 In insertion sort, at a time, elements can be moved ahead by one position only. To
move an element to a far-away position, many movements are required that
increase the algorithm's execution time. But shell sort overcomes this drawback of
insertion sort. It allows the movement and swapping of far-away elements as well.
 Note: if gap==1, the shell sort is same as insertion sort. If gap==0 , then we get the
sorted array.
 The performance and efficiency of shell depends on the gap sequence. Better the
gap sequence the less time would be take to sort the array. Some of the optimal
sequences / methods that can be used in the shell sort algorithm to find the gap/
interval are following:
 Out of that, we are going to take shell's
original sequence (N/2,N/4,N/8....1) as
intervals.
 Gap = floor value (n/2) method (ex: floor of
0.0 to 0.9 is 0)
 So in 1st pass gap1 = n/2
 In 2nd pass gap2 = n/4
 In 3rd pass gap3 = n/8 . .
 ……
 Until we get gap == 1
SHELL SORT COMPLEXIETY:
 Best Case Complexity - It occurs when there is no sorting required, i.e., the
array is already sorted. The best-case time complexity of Shell sort
is O(n*logn).
 Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of Shell sort is O(n*logn).
 Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array in
ascending order, but elements are in descending order. The worst-case time
complexity of Shell sort is O(n2).
 The space complexity of Shell sort is O(1).
INSERTION AND SHELL SORT ARE IDENTICAL?
In insertion sort , the element moves one position ahead to insert an element
at its correct position, whereas the shell sort exchanges the far elements.
The difference follows: while insertion sort works only with one sequence (i.e. from first element) and expands it;
shell sort has a diminishing increment where there is a gap between the compared elements (initially n/2). Hence
there are n/2 sequences to be sorted using insertion sort. In each step, the increment is shrunk and the number of
sequences is reduced. In the last step, there is no gap and the algorithm degenerates to simple insertion sort.
Because of the diminishing increment, the large and small elements are moved rapidly to correct part of the array
and than in the last step sorted using insertion sort really fast. This leads to reduced time complexity o(n^(4/3)).
 So a sorted list is 1-sorted. If the odd elements are sorted, and the even elements
are sorted, then the list is 2-sorted. An insertion sort basically skips to the last step
of shell sort. Hence shell sort is considered as a generalization of insertion sort.
 WHAT IS THE RELATIONSHIP B/W INSERTION SORT AND SHELL SORT?
 The shell sort, sometimes called the “diminishing increment
sort, improves on the insertion sort by breaking the original list
into a number of smaller sub-lists, each of which is sorted using
an insertion sort. The unique way that these sub-lists are chosen is the key to the shell sort.
 WHY IS SHELL SORT BETTER THAN INSERTION SORT?
Shell sort allows swapping of indexes that are far apart, n 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 exchange of far items. In
shell sort, we make the array h-sorted for a large value of h
HOW IS SHELL SORT A VARIATION OF INSERTION SORT?
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.
ADVANTAGES:
 Shell sort algorithm is only efficient for finite number of elements in an array.
 With improved average time complexity, it is a very efficient algorithm for medium size arrays.
 Shell sort algorithm is 5.32 x faster than bubble sort algorithm. Less number of swaps and comparisons are required
compared to insertion sort.
DISADVANTAGES:
 It is a complex algorithm. It is not stable sorting algorithm.
 Not that efficient as merge sort and quicksort.
 Limited to use for small size arrays.
 Shell sort algorithm is significantly slower than the merge sort, quick sort and heap sort algorithms.
APPLICATIONS:
 It performs more operations and has higher cache miss ratio.
 Since it can be implemented using little code and does not use the call stack, some
implementations of the osort function in the ‘C’ at embedded system.
 Similarly, an implementation of shell sort is present in the Linux kernel used in the uclibc
library.
 Shell sort can also serve as a sub-algorithm of introspective sort. This principle is employed
in the bzip2 compressor.
CONCLUSION:
Shell sort is one the most efficient sorting algorithm for large datasets. This
technique is based on insertion sort but it reduces the no. of swaps considerably. It
does not require extra memory which makes it an overall efficient algorithm for
sorting. It is used in C to sort the array by sorting pair of elements far apart from
each other and successively reducing gap between the elements to be stored.
Hence, we can say that it is not a stable sorting algorithm.
SHELL SORT-2.pptx

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Merge sort
Merge sortMerge sort
Merge sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Hash tables
Hash tablesHash tables
Hash tables
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Selection sort
Selection sortSelection sort
Selection sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Heap
HeapHeap
Heap
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 

Semelhante a SHELL SORT-2.pptx

shell and merge sort
shell and merge sortshell and merge sort
shell and merge sorti i
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Shell sorting
Shell sortingShell sorting
Shell sortingTUC
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.pptSushantRaj25
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 

Semelhante a SHELL SORT-2.pptx (20)

shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Sorting
SortingSorting
Sorting
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Selection sort
Selection sortSelection sort
Selection sort
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 

Mais de CS50Bootcamp

UNIT1-AI final.pptx
UNIT1-AI final.pptxUNIT1-AI final.pptx
UNIT1-AI final.pptxCS50Bootcamp
 
AI unit-2 lecture notes.docx
AI unit-2 lecture notes.docxAI unit-2 lecture notes.docx
AI unit-2 lecture notes.docxCS50Bootcamp
 
Coding and decoding.pptx
Coding and decoding.pptxCoding and decoding.pptx
Coding and decoding.pptxCS50Bootcamp
 
NETWORKING COMMANDS.pptx
NETWORKING COMMANDS.pptxNETWORKING COMMANDS.pptx
NETWORKING COMMANDS.pptxCS50Bootcamp
 
Ethical Hacking Workshop.pptx
Ethical Hacking Workshop.pptxEthical Hacking Workshop.pptx
Ethical Hacking Workshop.pptxCS50Bootcamp
 

Mais de CS50Bootcamp (6)

UNIT1-AI final.pptx
UNIT1-AI final.pptxUNIT1-AI final.pptx
UNIT1-AI final.pptx
 
AI unit-2 lecture notes.docx
AI unit-2 lecture notes.docxAI unit-2 lecture notes.docx
AI unit-2 lecture notes.docx
 
Coding and decoding.pptx
Coding and decoding.pptxCoding and decoding.pptx
Coding and decoding.pptx
 
NETWORKING COMMANDS.pptx
NETWORKING COMMANDS.pptxNETWORKING COMMANDS.pptx
NETWORKING COMMANDS.pptx
 
Ethical Hacking Workshop.pptx
Ethical Hacking Workshop.pptxEthical Hacking Workshop.pptx
Ethical Hacking Workshop.pptx
 
PERMUTATIONS.pptx
PERMUTATIONS.pptxPERMUTATIONS.pptx
PERMUTATIONS.pptx
 

Último

"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.MdManikurRahman
 
solid state electronics ktu module 5 slides
solid state electronics ktu module 5 slidessolid state electronics ktu module 5 slides
solid state electronics ktu module 5 slidesARUN AV
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdfKamal Acharya
 
How to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdfHow to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdftawat puangthong
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdfKamal Acharya
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxMonirHossain707319
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AISheetal Jain
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...MohammadAliNayeem
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdfKamal Acharya
 
Attraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxAttraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxkarthikeyanS725446
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5T.D. Shashikala
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfJNTUA
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor banktawat puangthong
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationDr. Radhey Shyam
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxwendy cai
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdfKamal Acharya
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineAftabkhan575376
 
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxGagandeepKaur617299
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdfKamal Acharya
 

Último (20)

"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.
 
solid state electronics ktu module 5 slides
solid state electronics ktu module 5 slidessolid state electronics ktu module 5 slides
solid state electronics ktu module 5 slides
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
How to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdfHow to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdf
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptx
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdf
 
Attraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxAttraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptx
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
 
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdf
 

SHELL SORT-2.pptx

  • 1.
  • 2. AKASH GUPTA LIKHITH AJEETH KUMAR P VIKRAM ANUPA ROHIT BHUSHAN B NANDU A MAHESH IDREES SHEIK DENNIS
  • 3. SORT TYPES OF SORT HISTORY AND USAGE ALGORITHM PRINCIPLES OF WORKING COMPLEXITY ADVANTAGES AND DRAWBACKS APPLICATIONS CONCLUSION OUTLINE
  • 4. SORT BEFORE WE START WITH SHELL SORT, LET US FIRST KNOW ABOUT SORTING  What is sorting?  Sorting is the process of arranging elements either in ascending (or) descending order. The term sorting came into existence when humans realized the importance of searching quickly.  Why do we use sorting?  A sorting algorithm will put items in a list into an order, such as alphabetical or numerical order. For example, a list of customer names could be sorted into alphabetical order by surname, or a list of people could be put into numerical order by age.
  • 6. THE SORT WE CHOSE TO EXPLAIN IS THE SHELL SORT:  BASICALLY IF WE ASK ANYONE OVER HERE ABOUT THE TYPES OF SORTS, THEY SAY THE TYPES WE PREVIOUSLY LEARNT. BUT THE SHELL SORT IS THE TYPE WHERE WE NEVER HEARD OR LEARNT IN OUR EARLIER STAGES OF EDUCATION.  So what exactly is shell sort?  Shell sort is the generalization of the insertion sort. It first sorts elements that are far apart from each other and then reduces the gap between the elements to be sorted. Gap reduction is based on the sequence used. It is basically faster than the other sorts. We use this sort to fasten our process.
  • 7. HISTORY AND THE BASICS OF SHELL SORT The shell sort (also known as shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959. This was invented at university of Cincinnati. Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The increment size is reduced after each pass until the increment size is 1. With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case". Any sequence will sort the data as long as it ends in 1, but some sequences work better than others.
  • 8. WHY DO WE USE SHELL SORT The shell sort is used to sort the array by sorting the pair of elements far apart from each other depending on it's gap/interval and reduces the gap between the elements to be sorted. It can transfer the disordered elements into the correct place faster than any other sort. Shell sort has improved the average time complexity of insertion sort. As similar to insertion sort, it is a comparison-based and in-place sorting algorithm. Shell sort is efficient for medium-sized data sets. This algorithm first sorts the elements that are far away from each other, then it subsequently reduces the gap between them. This gap is called as interval. This interval can be calculated by using the Knuth's formula given below - HH = H * 3 + 1 (where, 'h' is the interval having initial value 1)
  • 9. THE ALGORITHM & HOW DOES IT WORK?  Let's see the working of the shell sort algorithm. To understand the working of the shell sort algorithm, let’s take an unsorted array. It will be easier to understand the shell sort via an example. • Let the elements of array be – 23 29 15 19 31 7 9 5 2  We will use the original sequence of shell sort, i.e. N/2, n/4,....,1 as the intervals.  Step-1: Insert all the elements in array a[20]  Step-2: In 1st pass, Gap= ⌊N/2⌋ = ⌊9/2⌋ = ⌊4.5⌋ = 4  So the elements of the gap/interval of 4 are compared and swapped if they are out of order in the first gap in both forward & backward directions.  Here, 0th element and the 4th element are compared. 23 29 15 19 31 7 9 5 2 0 1 2 3 4 5 6 7 8 INDEX -> ELEMENTS->
  • 10.  If the 0th element > 4th element then both are swapped.  If the 0th element == 4th element then no swapping is required.  The rearranged elements in an n/2 interval: Array: [ 23 29 15 19 31 7 9 5 2 ]  Similarly, this procedure is repeated for all remaining elements & rearranges the elements in an n/2 interval, i.e 1st 5th 2nd 6th 3rd 7th 4th 0th
  • 11. 23 29 15 19 31 7 9 5 2 23 7 15 19 31 29 9 5 2 23 7 9 19 31 29 15 5 2 23 7 9 5 31 29 15 19 2 23 7 9 5 2 29 15 19 31 2 7 9 5 23 29 15 19 31 PASS-1 RESULT OF PASS-1:
  • 12.  Step-3: In 2nd pass, gap = ⌊N/4⌋ = ⌊9/4⌋ = ⌊2.5⌋ = 2 [2 is the chosen interval]  Here, the elements are sorted & stored which are at the gap of 2.  Until J < = N – 1, run 2 7 9 5 23 29 15 19 31 2 5 9 7 23 29 15 19 31 2 5 9 7 15 29 23 19 31 2 5 9 7 15 19 23 29 31  Compare 0th element with 2nd element and go on…. 0 1 2 3 4 5 6 7 8 2 7 9 5 23 29 15 19 31 In N/4 Interval Result Of Pass-2 INDEX -> ELEMENTS->
  • 13.  Step-4: In 3rd pass  Here, shell sort works same as insertion sort  Gap = ⌊N/8⌋ = ⌊9/8⌋ = ⌊1.11⌋ = 1 2 5 9 7 15 19 23 29 31  Step-5: Gap = ⌊n/16⌋ = 0  We can see the sorted array of elements. 2 5 9 7 15 19 23 29 31
  • 15. CODE SNIPPET OF SHELL SORT: int main() { int a[20],i,n; printf("Enter number of elements:"); scanf("%d", &n); printf("Enter array elements:n"); for(i=0;i<n;++i) scanf("%d",&a[i]); sort(a, n); printf("n Array after shell sort:n"); for(i=0;i<n;++i) printf("%d ",a[i]); return 0; } #include<stdio.h> void sort(int a[],int n) { int gap, i, j, temp; for(gap=n/2;gap>0;gap/=2) { for(i = gap; i < n; i+=1) { temp=a[i]; for(j=i; j>=gap&&a[j-gap]>temp; j-=gap)a[j]=a[j-gap];a[j]=temp; } } } Output: Enter number of elements: 5 Enter array elements: 5 6 7 2 9 12 Array after shell sort: 2 7 9 12 56
  • 16.
  • 17.
  • 18. THE PRINCIPLES OF ITS WORKING:  In insertion sort, at a time, elements can be moved ahead by one position only. To move an element to a far-away position, many movements are required that increase the algorithm's execution time. But shell sort overcomes this drawback of insertion sort. It allows the movement and swapping of far-away elements as well.  Note: if gap==1, the shell sort is same as insertion sort. If gap==0 , then we get the sorted array.  The performance and efficiency of shell depends on the gap sequence. Better the gap sequence the less time would be take to sort the array. Some of the optimal sequences / methods that can be used in the shell sort algorithm to find the gap/ interval are following:
  • 19.  Out of that, we are going to take shell's original sequence (N/2,N/4,N/8....1) as intervals.  Gap = floor value (n/2) method (ex: floor of 0.0 to 0.9 is 0)  So in 1st pass gap1 = n/2  In 2nd pass gap2 = n/4  In 3rd pass gap3 = n/8 . .  ……  Until we get gap == 1
  • 20. SHELL SORT COMPLEXIETY:  Best Case Complexity - It occurs when there is no sorting required, i.e., the array is already sorted. The best-case time complexity of Shell sort is O(n*logn).  Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of Shell sort is O(n*logn).  Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse order. That means suppose you have to sort the array in ascending order, but elements are in descending order. The worst-case time complexity of Shell sort is O(n2).  The space complexity of Shell sort is O(1).
  • 21. INSERTION AND SHELL SORT ARE IDENTICAL? In insertion sort , the element moves one position ahead to insert an element at its correct position, whereas the shell sort exchanges the far elements. The difference follows: while insertion sort works only with one sequence (i.e. from first element) and expands it; shell sort has a diminishing increment where there is a gap between the compared elements (initially n/2). Hence there are n/2 sequences to be sorted using insertion sort. In each step, the increment is shrunk and the number of sequences is reduced. In the last step, there is no gap and the algorithm degenerates to simple insertion sort. Because of the diminishing increment, the large and small elements are moved rapidly to correct part of the array and than in the last step sorted using insertion sort really fast. This leads to reduced time complexity o(n^(4/3)).
  • 22.  So a sorted list is 1-sorted. If the odd elements are sorted, and the even elements are sorted, then the list is 2-sorted. An insertion sort basically skips to the last step of shell sort. Hence shell sort is considered as a generalization of insertion sort.  WHAT IS THE RELATIONSHIP B/W INSERTION SORT AND SHELL SORT?  The shell sort, sometimes called the “diminishing increment sort, improves on the insertion sort by breaking the original list into a number of smaller sub-lists, each of which is sorted using an insertion sort. The unique way that these sub-lists are chosen is the key to the shell sort.
  • 23.  WHY IS SHELL SORT BETTER THAN INSERTION SORT? Shell sort allows swapping of indexes that are far apart, n 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 exchange of far items. In shell sort, we make the array h-sorted for a large value of h HOW IS SHELL SORT A VARIATION OF INSERTION SORT? 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.
  • 24. ADVANTAGES:  Shell sort algorithm is only efficient for finite number of elements in an array.  With improved average time complexity, it is a very efficient algorithm for medium size arrays.  Shell sort algorithm is 5.32 x faster than bubble sort algorithm. Less number of swaps and comparisons are required compared to insertion sort. DISADVANTAGES:  It is a complex algorithm. It is not stable sorting algorithm.  Not that efficient as merge sort and quicksort.  Limited to use for small size arrays.  Shell sort algorithm is significantly slower than the merge sort, quick sort and heap sort algorithms.
  • 25. APPLICATIONS:  It performs more operations and has higher cache miss ratio.  Since it can be implemented using little code and does not use the call stack, some implementations of the osort function in the ‘C’ at embedded system.  Similarly, an implementation of shell sort is present in the Linux kernel used in the uclibc library.  Shell sort can also serve as a sub-algorithm of introspective sort. This principle is employed in the bzip2 compressor.
  • 26. CONCLUSION: Shell sort is one the most efficient sorting algorithm for large datasets. This technique is based on insertion sort but it reduces the no. of swaps considerably. It does not require extra memory which makes it an overall efficient algorithm for sorting. It is used in C to sort the array by sorting pair of elements far apart from each other and successively reducing gap between the elements to be stored. Hence, we can say that it is not a stable sorting algorithm.