SlideShare a Scribd company logo
1 of 26
Welcome
University of Science & Technology,
Chittagong
Faculty of Science, Engineering &
Technology
Department: Computer Science &
Engineering
Course Title: Algorithm lab
Course code: CSE 222
Semester: 4th
Batch: 25th
Submitted by:
Name: Md Abdul Kuddus
Roll: 15010102
Submitted to:
Sohrab Hossain , Assistant Professor,
Department of CSE,FSET,USTC
Presentation Topic:
MERGE SORT
MERGE SORT :
Merge sort was invented by John Von Neumann
(1903 - 1957)
Merge sort is divide & conquer technique of sorting
element
Merge sort is one of the most efficient sorting
algorithm
Time complexity of merge sort is O(n log n)
DIVIDE & CONQUER
• DIVIDE : Divide the unsorted list into two sub lists of
about half the size
• CONQUER : Sort each of the two sub lists recursively.
If they are small enough just solve them in a straight
forward manner
• COMBINE : Merge the two-sorted sub lists back into
one sorted list
DIVIDE & CONQUER TECHNIQUE
a problem of size n
sub problem 1 of size n/2
sub problem 2 of
size n/2
a solution to sub
problem 1
a solution to sub
problem 2
a solution to the
original problem
MERGE SORT PROCESS :
• The list is divided into two equal (as equal as
possible) part
• There are different ways to divide the list into two
equal part
• The following algorithm divides the list until the list
has just one item which are sorted
• Then with recursive merge function calls these
smaller pieces merge
MERGE SORT ALGORITHM :
• Merge(A[],p , q , r)
{ n1 = q – p + 1
n2 = r – q
Let L[1 to n1+1] and R[1 to n2+1] be new array
for(i = 1 to n1)
L[i] = A[p + i - 1]
for(j = 1 to n2)
R[j] = A[q + j]
L[n1 + 1] = infinity
R[n2 + 1] = infinity
for(k = p to r)
{ if ( L[i] <= R[j])
A[k] = L[j]
i = i + 1
else
A[k] = R[j]
j = j + 1
}
Merge sort (A, p, r)
{ if (p < r) // check for base case
q = [ (p + r)/2 ] // divide step
Merge sort (A, p, q) // conquer step
Merge sort (A, q+1, r) // conquer step
Merge sort (A, p, q, r) // conquer step
}
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1K
1 2 3 4 5 6 7 8
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7 8K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7 8 9K
/* Merging (Merge sort) */
#include <stdio.h>
void main ()
{
int a[10] = {1, 5, 11, 30, 2, 4, 6, 9};
int i,j,k,n1,n2,p,q,r;
p = 1; q = 4; r = 8; n1 = q-p+1; n2 = r-q;
int L[n1+1];
int R[n2+1];
for( i=0; i<n1; i++)
L[i] = a[p+i-1];
for( i=0; i<n1; i++)
printf("%d n", L[i]);
for( j=0; j<n2; j++)
R[j] = a[q+j];
L[n1] = 300; R[n2] = 300; i=0; j=0;
for( k=0; k<r; k++)
{
if (L[i] <= R[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = R[j];
j++;
IMPLEMENTING MERGE SORT :
There are two basic ways to implement
merge sort
In place : Merge sort is done with only the
input array
Double storage : Merging is done with a
temporary array of the same size as the input
array
MERGE-SORT ANALYSIS :
Time, merging
log n levels
Total time for merging : O (n log n)
Total running time : Order of n log n
Total space : Order of n
n/2 n/2
n/4 n/4 n/4 n/4
n
Performance of MERGESORT :
• Unlike quick sort, merge sort guarantees O (n log n)in
the worst case
• The reason for this is that quick sort depends on the
value of the pivot whereas merge sort divides the list
based on the index
• Why is it O (n log n ) ?
• Each merge will require N comparisons
• Each time the list is halved
• So the standard divide & conquer recurrence applies
to merge sort
T(n) = 2T * n/2 + (n)
FEATURES OF MERGE SORT :
• It perform in O(n log n ) in the worst case
• It is stable
• It is quite independent of the way the initial list is organized
• Good for linked lists. Can be implemented in such a way that
data is accessed sequentially.
Drawbacks :
It may require an array of up to the size of the original list.
This can be avoided but the algorithm becomes significantly
more complicated making it worth while.
Instead of making it complicated we can use HEAP SORT which
is also O(n log n ). But you have to remember that HEAP SORT is not
stable in comparison to MERGE SORT
QUERIES
?
THANK YOU

More Related Content

What's hot

Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |MdSaiful14
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 

What's hot (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Quick sort
Quick sortQuick sort
Quick sort
 
Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Shell sort
Shell sortShell sort
Shell sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 

Similar to Merge sort algorithm power point presentation

Similar to Merge sort algorithm power point presentation (20)

Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
Mergesort
MergesortMergesort
Mergesort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
Merge sort
Merge sortMerge sort
Merge sort
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Parallel sorting algorithm
Parallel sorting algorithmParallel sorting algorithm
Parallel sorting algorithm
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
 
sorting
sortingsorting
sorting
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004
 

Recently uploaded

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 

Recently uploaded (20)

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 

Merge sort algorithm power point presentation

  • 2. University of Science & Technology, Chittagong Faculty of Science, Engineering & Technology Department: Computer Science & Engineering Course Title: Algorithm lab Course code: CSE 222 Semester: 4th Batch: 25th
  • 3. Submitted by: Name: Md Abdul Kuddus Roll: 15010102 Submitted to: Sohrab Hossain , Assistant Professor, Department of CSE,FSET,USTC
  • 5. MERGE SORT : Merge sort was invented by John Von Neumann (1903 - 1957) Merge sort is divide & conquer technique of sorting element Merge sort is one of the most efficient sorting algorithm Time complexity of merge sort is O(n log n)
  • 6. DIVIDE & CONQUER • DIVIDE : Divide the unsorted list into two sub lists of about half the size • CONQUER : Sort each of the two sub lists recursively. If they are small enough just solve them in a straight forward manner • COMBINE : Merge the two-sorted sub lists back into one sorted list
  • 7. DIVIDE & CONQUER TECHNIQUE a problem of size n sub problem 1 of size n/2 sub problem 2 of size n/2 a solution to sub problem 1 a solution to sub problem 2 a solution to the original problem
  • 8. MERGE SORT PROCESS : • The list is divided into two equal (as equal as possible) part • There are different ways to divide the list into two equal part • The following algorithm divides the list until the list has just one item which are sorted • Then with recursive merge function calls these smaller pieces merge
  • 9. MERGE SORT ALGORITHM : • Merge(A[],p , q , r) { n1 = q – p + 1 n2 = r – q Let L[1 to n1+1] and R[1 to n2+1] be new array for(i = 1 to n1) L[i] = A[p + i - 1] for(j = 1 to n2) R[j] = A[q + j] L[n1 + 1] = infinity R[n2 + 1] = infinity
  • 10. for(k = p to r) { if ( L[i] <= R[j]) A[k] = L[j] i = i + 1 else A[k] = R[j] j = j + 1 }
  • 11. Merge sort (A, p, r) { if (p < r) // check for base case q = [ (p + r)/2 ] // divide step Merge sort (A, p, q) // conquer step Merge sort (A, q+1, r) // conquer step Merge sort (A, p, q, r) // conquer step }
  • 12. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1K 1 2 3 4 5 6 7 8
  • 13. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2K
  • 14. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4K
  • 15. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5K
  • 16. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6K
  • 17. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7K
  • 18. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7 8K
  • 19. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7 8 9K
  • 20. /* Merging (Merge sort) */ #include <stdio.h> void main () { int a[10] = {1, 5, 11, 30, 2, 4, 6, 9}; int i,j,k,n1,n2,p,q,r; p = 1; q = 4; r = 8; n1 = q-p+1; n2 = r-q; int L[n1+1]; int R[n2+1]; for( i=0; i<n1; i++) L[i] = a[p+i-1]; for( i=0; i<n1; i++) printf("%d n", L[i]); for( j=0; j<n2; j++) R[j] = a[q+j];
  • 21. L[n1] = 300; R[n2] = 300; i=0; j=0; for( k=0; k<r; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; j++;
  • 22. IMPLEMENTING MERGE SORT : There are two basic ways to implement merge sort In place : Merge sort is done with only the input array Double storage : Merging is done with a temporary array of the same size as the input array
  • 23. MERGE-SORT ANALYSIS : Time, merging log n levels Total time for merging : O (n log n) Total running time : Order of n log n Total space : Order of n n/2 n/2 n/4 n/4 n/4 n/4 n
  • 24. Performance of MERGESORT : • Unlike quick sort, merge sort guarantees O (n log n)in the worst case • The reason for this is that quick sort depends on the value of the pivot whereas merge sort divides the list based on the index • Why is it O (n log n ) ? • Each merge will require N comparisons • Each time the list is halved • So the standard divide & conquer recurrence applies to merge sort T(n) = 2T * n/2 + (n)
  • 25. FEATURES OF MERGE SORT : • It perform in O(n log n ) in the worst case • It is stable • It is quite independent of the way the initial list is organized • Good for linked lists. Can be implemented in such a way that data is accessed sequentially. Drawbacks : It may require an array of up to the size of the original list. This can be avoided but the algorithm becomes significantly more complicated making it worth while. Instead of making it complicated we can use HEAP SORT which is also O(n log n ). But you have to remember that HEAP SORT is not stable in comparison to MERGE SORT