SlideShare a Scribd company logo
1 of 12
Shell Sort is a generalized version of insertion sort. It is an
in–place comparison sort. Shell Sort is also known as
diminishing increment sort, it is one of the oldest sorting
algorithms invented by Donald L. Shell (1959.)
This algorithm uses insertion sort on the large interval of
elements to sort. Then the interval of sorting keeps on
decreasing in a sequence until the interval reaches 1. These
intervals are known as gap sequence.
This algorithm works quite efficiently for small and medium
size array as its average time complexity is near to O(n).
OVERVIEW
• Shell Sort is a comparison based sorting.
• Time complexity of Shell Sort depends on gap sequence . Its best case time
complexity is O(n* logn) and worst case is O(n* log2n). Time complexity of Shell
sort is generally assumed to be near to O(n) and less than O(n2) as determining its
time complexity is still an open problem.
• The best case in shell sort is when the array is already sorted. The number of
comparisons is less.
• It is an in-place sorting algorithm as it requires no additional scratch space.
• Shell Sort is unstable sort as relative order of elements with equal values may
change.
• It is been observed that shell sort is 5 times faster than bubble sort and twice faster
than insertion sort its closest competitor.
• There are various increment sequences or gap sequences in shell sort which
produce various complexity between O(n) and O(n2).
FEATURES:
78 15 6 91 88 12 7 32 5 111 47
n = 11
gap = floor(n / 2) = floor(11/2) = 5
gap = 5
78156 918812 7 325 11147
gap = 5
78156 918812 7 325 11147
gap = 2
SHELL-SORT(A,n)
// we take gap sequence in order of |N/2|, |N/4|, |N/8|...1
for gap=n/2; gap=0; gap/=2 do:
//Perform gapped insertion sort for this gap size.
for i=gap; i<n; i+=1 do: temp=A[i]
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
for j=i; j>=gap && A[j-gap]>temp;j-=gap
do:
A[j]= A[j-gap]
end for
// put temp in its correct location
A[j]= temp;
end for
end for
end func
PSEUDOCODE
void shellSort(int A[], int n)
{
int gap,i;
// Start with a larger gap, then reduce the gap to 1
// we take gap sequence in order of |N/2|, |N/4|, |N/8|...1
for (gap = n/2; gap > 0; gap /= 2)
{
// we performe 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) {
// store a[i] in temp and make a hole at position i
int temp = A[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && A[j - gap] > temp; j -= gap)
A[j] = A[j - gap];
// put temp (the original a[i]) in its correct location
A[j] = temp;
}
}
}
C-Implementation
Since in this algorithm insertion sort is applied in the large interval of elements
and then interval reduces in a sequence, therefore the running time of Shell sort
is heavily dependent on the gap sequence it uses .Summarising all this –
Worst Case Time complexity: O (n2)
Average Case Time complexity: depends on gap sequence.
Best Case Time complexity: O(n*logn)
Worst Case Space Complexity: O(n) total, O(1) auxiliary
Data Structure: Array
Sorting In Place: Yes
Stable: No
Asymptotic Analysis
Merge Sort is a divide and conquers algorithm in which original data is divided
into a smaller set of data to sort the array.
In merge sort the array is firstly divided into two halves, and then further sub-
arrays are recursively divided into two halves till we get N sub-arrays, each
containing 1 element.
Then, the sub-arrays are repeatedly merged, to produce new array until there is
one array remaining. This will be our sorted array at the end.
OVERVIEW
• Merge Sort is a type of recursive algorithm.
• We can express time complexity of merge sort by this recurrence relation: T(n) =
2T(n/2) + O(n)
Using Masters Theorem, we get -> T(n)=O(n*logn).
• Time complexity of Merge Sort is O(n*logn) in all 3 cases (worst, average and best)
as in merge sort , array is recursively divided into two halves and take linear time to
merge two halves.
• It is not an in-place sorting algorithm as it requires additional scratch space
proportional to the size of the input array.
• It requires an equal amount of additional space as the unsorted list. Hence, it’s not
at all recommended for sorting large size list.
• Merge Sort is a stable sort, which means the “equal” elements appear in the same
order in the sorted array as they were in the unsorted array.
• Merge Sort is a best-sorting technique for sorting Linked Lists. Because in linked
list it can be implemented without extra space as elements can be inserted in the
middle in O(1) extra space and O(1) time.
FEATURES:
//A->array, l->left most index, r->right most index
MERGE-SORT(A, l, r)
if l < r
mid = (l+(r-l)/2)
MERGE-SORT(A, l, mid)
MERGE-SORT (A, mid+1, r)
MERGE(A, l, mid ,r)
end func
MERGE(A, l, m, r)
nL = m-l+1
nR = r-m
Create arrays L[1..nL+1] and R[1..nR+1]
for i=0 to nL-1
L[i] = A[l+i]
end for
for j=0 to nR-1
R[j] = A[m+l+j]
end for
i=0; j=0; k=l;
while i < nL and j < nR
if L[i] <= R[j]
A[k]=L[i]; i=i+1; k=k+1;
else
A[k]=R[j]; j=j+1; k=k+1;
end while
while i < nL
A[k]=L[i]; i=i+1; k=k+1;
end while
while j < nR
A[k]=R[j]; j=j+1; k=k+1;
end while
end func
PSEUDOCODE
void merge_sort(int *a,int len_a,int *b,int len_b) {
if (len_a > 1) {
int mid_a = len_a >> 1;
merge_sort(a,mid_a,a+mid_a,len_a-mid_a);
}
if (len_b > 1) {
int mid_b = len_b >> 1;
merge_sort(b,mid_b,b+mid_b,len_b-mid_b);
}
int curr_a=0, curr_b=0, *temp_arr,k=0;
temp_arr = (int*)malloc((len_a+len_b)*sizeof(int));
while (curr_a < len_a && curr_b < len_b) {
if (a[curr_a] <= b[curr_b]) {
temp_arr[k]=a[curr_a];
curr_a++;
}
else {
temp_arr[k]=b[curr_b];
curr_b++;
}
k++;
}
while (curr_a < len_a) {
temp_arr[k] = a[curr_a];
curr_a++;
k++;
}
while (curr_b < len_b) {
temp_arr[k] = b[curr_b];
curr_b++;
k++;
}
for (int k=0;k<len_a+len_b;k++){
a[k] = temp_arr[k];
}
free(temp_arr);
}
C-IMPLEMENTATION
Since in this algorithm array is recursively divided into two halves and take linear
time to merge two halves, so the complexity of this algorithm id O(n*logn) in all
the cases. Summarizing all this-
Worst Case Time complexity: O (n*logn)
Average Case Time complexity: O(n*logn)
Best Case Time complexity: O(n*logn)
Space Complexity: O(n) Auxiliary space
Algorithmic Paradigm: Divide and Conquer
Sorting In Place: No
Space Complexity: O(n)
Stable: Yes
Asymptotic Analysis
Source:
http://www.codingeek.com/algorithms/shell-sort-algorithm-explanation-implementation-and-complexity/
http://www.codingeek.com/algorithms/merge-sort-algorithm-explanation-implementation-and-complexity/

More Related Content

What's hot (20)

Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Hamming codes
Hamming codesHamming codes
Hamming codes
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
Neuro-fuzzy systems
Neuro-fuzzy systemsNeuro-fuzzy systems
Neuro-fuzzy systems
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Church Turing Thesis
Church Turing ThesisChurch Turing Thesis
Church Turing Thesis
 
Naming in Distributed Systems
Naming in Distributed SystemsNaming in Distributed Systems
Naming in Distributed Systems
 
Red black tree
Red black treeRed black tree
Red black tree
 
lattice
 lattice lattice
lattice
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Parallel searching
Parallel searchingParallel searching
Parallel searching
 

Similar to shell and merge sort

Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sortingVivek Bhargav
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxkassahungebrie
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptxworkwithiec
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannualmaamir farooq
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxdudelover
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmEhsan Ehrari
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sortijfcstjournal
 

Similar to shell and merge sort (20)

Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
 
SHELL SORT-2.pptx
SHELL SORT-2.pptxSHELL SORT-2.pptx
SHELL SORT-2.pptx
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptx
 
Sorting
SortingSorting
Sorting
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
sorting
sortingsorting
sorting
 
Square Root Decomposition
Square Root DecompositionSquare Root Decomposition
Square Root Decomposition
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sort
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 

More from i i

Bouncing circle
Bouncing circleBouncing circle
Bouncing circlei i
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
 
sequential and combinational circuits exam
sequential and combinational circuits examsequential and combinational circuits exam
sequential and combinational circuits exami i
 
hypothesis testing overview
hypothesis testing overviewhypothesis testing overview
hypothesis testing overviewi i
 
x86 architecture
x86 architecturex86 architecture
x86 architecturei i
 
boolean algebra exercises
boolean algebra exercisesboolean algebra exercises
boolean algebra exercisesi i
 
database normalization case study
database normalization case studydatabase normalization case study
database normalization case studyi i
 
cpbricks context diagram
cpbricks context diagramcpbricks context diagram
cpbricks context diagrami i
 
cpbricks project document
cpbricks project documentcpbricks project document
cpbricks project documenti i
 
cpbricks manual
cpbricks manualcpbricks manual
cpbricks manuali i
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, androidi i
 
shortest job first
shortest job firstshortest job first
shortest job firsti i
 
designing reports
designing reportsdesigning reports
designing reportsi i
 
bnf of c switch statement
bnf of c switch statementbnf of c switch statement
bnf of c switch statementi i
 
adders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAadders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAi i
 

More from i i (15)

Bouncing circle
Bouncing circleBouncing circle
Bouncing circle
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
sequential and combinational circuits exam
sequential and combinational circuits examsequential and combinational circuits exam
sequential and combinational circuits exam
 
hypothesis testing overview
hypothesis testing overviewhypothesis testing overview
hypothesis testing overview
 
x86 architecture
x86 architecturex86 architecture
x86 architecture
 
boolean algebra exercises
boolean algebra exercisesboolean algebra exercises
boolean algebra exercises
 
database normalization case study
database normalization case studydatabase normalization case study
database normalization case study
 
cpbricks context diagram
cpbricks context diagramcpbricks context diagram
cpbricks context diagram
 
cpbricks project document
cpbricks project documentcpbricks project document
cpbricks project document
 
cpbricks manual
cpbricks manualcpbricks manual
cpbricks manual
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
shortest job first
shortest job firstshortest job first
shortest job first
 
designing reports
designing reportsdesigning reports
designing reports
 
bnf of c switch statement
bnf of c switch statementbnf of c switch statement
bnf of c switch statement
 
adders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAadders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISA
 

Recently uploaded

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 

Recently uploaded (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 

shell and merge sort

  • 1. Shell Sort is a generalized version of insertion sort. It is an in–place comparison sort. Shell Sort is also known as diminishing increment sort, it is one of the oldest sorting algorithms invented by Donald L. Shell (1959.) This algorithm uses insertion sort on the large interval of elements to sort. Then the interval of sorting keeps on decreasing in a sequence until the interval reaches 1. These intervals are known as gap sequence. This algorithm works quite efficiently for small and medium size array as its average time complexity is near to O(n). OVERVIEW
  • 2. • Shell Sort is a comparison based sorting. • Time complexity of Shell Sort depends on gap sequence . Its best case time complexity is O(n* logn) and worst case is O(n* log2n). Time complexity of Shell sort is generally assumed to be near to O(n) and less than O(n2) as determining its time complexity is still an open problem. • The best case in shell sort is when the array is already sorted. The number of comparisons is less. • It is an in-place sorting algorithm as it requires no additional scratch space. • Shell Sort is unstable sort as relative order of elements with equal values may change. • It is been observed that shell sort is 5 times faster than bubble sort and twice faster than insertion sort its closest competitor. • There are various increment sequences or gap sequences in shell sort which produce various complexity between O(n) and O(n2). FEATURES:
  • 3. 78 15 6 91 88 12 7 32 5 111 47 n = 11 gap = floor(n / 2) = floor(11/2) = 5 gap = 5 78156 918812 7 325 11147 gap = 5 78156 918812 7 325 11147 gap = 2
  • 4. SHELL-SORT(A,n) // we take gap sequence in order of |N/2|, |N/4|, |N/8|...1 for gap=n/2; gap=0; gap/=2 do: //Perform gapped insertion sort for this gap size. for i=gap; i<n; i+=1 do: temp=A[i] // shift earlier gap-sorted elements up until // the correct location for a[i] is found for j=i; j>=gap && A[j-gap]>temp;j-=gap do: A[j]= A[j-gap] end for // put temp in its correct location A[j]= temp; end for end for end func PSEUDOCODE
  • 5. void shellSort(int A[], int n) { int gap,i; // Start with a larger gap, then reduce the gap to 1 // we take gap sequence in order of |N/2|, |N/4|, |N/8|...1 for (gap = n/2; gap > 0; gap /= 2) { // we performe 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) { // store a[i] in temp and make a hole at position i int temp = A[i]; // shift earlier gap-sorted elements up until the correct // location for a[i] is found int j; for (j = i; j >= gap && A[j - gap] > temp; j -= gap) A[j] = A[j - gap]; // put temp (the original a[i]) in its correct location A[j] = temp; } } } C-Implementation
  • 6. Since in this algorithm insertion sort is applied in the large interval of elements and then interval reduces in a sequence, therefore the running time of Shell sort is heavily dependent on the gap sequence it uses .Summarising all this – Worst Case Time complexity: O (n2) Average Case Time complexity: depends on gap sequence. Best Case Time complexity: O(n*logn) Worst Case Space Complexity: O(n) total, O(1) auxiliary Data Structure: Array Sorting In Place: Yes Stable: No Asymptotic Analysis
  • 7. Merge Sort is a divide and conquers algorithm in which original data is divided into a smaller set of data to sort the array. In merge sort the array is firstly divided into two halves, and then further sub- arrays are recursively divided into two halves till we get N sub-arrays, each containing 1 element. Then, the sub-arrays are repeatedly merged, to produce new array until there is one array remaining. This will be our sorted array at the end. OVERVIEW
  • 8. • Merge Sort is a type of recursive algorithm. • We can express time complexity of merge sort by this recurrence relation: T(n) = 2T(n/2) + O(n) Using Masters Theorem, we get -> T(n)=O(n*logn). • Time complexity of Merge Sort is O(n*logn) in all 3 cases (worst, average and best) as in merge sort , array is recursively divided into two halves and take linear time to merge two halves. • It is not an in-place sorting algorithm as it requires additional scratch space proportional to the size of the input array. • It requires an equal amount of additional space as the unsorted list. Hence, it’s not at all recommended for sorting large size list. • Merge Sort is a stable sort, which means the “equal” elements appear in the same order in the sorted array as they were in the unsorted array. • Merge Sort is a best-sorting technique for sorting Linked Lists. Because in linked list it can be implemented without extra space as elements can be inserted in the middle in O(1) extra space and O(1) time. FEATURES:
  • 9. //A->array, l->left most index, r->right most index MERGE-SORT(A, l, r) if l < r mid = (l+(r-l)/2) MERGE-SORT(A, l, mid) MERGE-SORT (A, mid+1, r) MERGE(A, l, mid ,r) end func MERGE(A, l, m, r) nL = m-l+1 nR = r-m Create arrays L[1..nL+1] and R[1..nR+1] for i=0 to nL-1 L[i] = A[l+i] end for for j=0 to nR-1 R[j] = A[m+l+j] end for i=0; j=0; k=l; while i < nL and j < nR if L[i] <= R[j] A[k]=L[i]; i=i+1; k=k+1; else A[k]=R[j]; j=j+1; k=k+1; end while while i < nL A[k]=L[i]; i=i+1; k=k+1; end while while j < nR A[k]=R[j]; j=j+1; k=k+1; end while end func PSEUDOCODE
  • 10. void merge_sort(int *a,int len_a,int *b,int len_b) { if (len_a > 1) { int mid_a = len_a >> 1; merge_sort(a,mid_a,a+mid_a,len_a-mid_a); } if (len_b > 1) { int mid_b = len_b >> 1; merge_sort(b,mid_b,b+mid_b,len_b-mid_b); } int curr_a=0, curr_b=0, *temp_arr,k=0; temp_arr = (int*)malloc((len_a+len_b)*sizeof(int)); while (curr_a < len_a && curr_b < len_b) { if (a[curr_a] <= b[curr_b]) { temp_arr[k]=a[curr_a]; curr_a++; } else { temp_arr[k]=b[curr_b]; curr_b++; } k++; } while (curr_a < len_a) { temp_arr[k] = a[curr_a]; curr_a++; k++; } while (curr_b < len_b) { temp_arr[k] = b[curr_b]; curr_b++; k++; } for (int k=0;k<len_a+len_b;k++){ a[k] = temp_arr[k]; } free(temp_arr); } C-IMPLEMENTATION
  • 11. Since in this algorithm array is recursively divided into two halves and take linear time to merge two halves, so the complexity of this algorithm id O(n*logn) in all the cases. Summarizing all this- Worst Case Time complexity: O (n*logn) Average Case Time complexity: O(n*logn) Best Case Time complexity: O(n*logn) Space Complexity: O(n) Auxiliary space Algorithmic Paradigm: Divide and Conquer Sorting In Place: No Space Complexity: O(n) Stable: Yes Asymptotic Analysis