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 (20)

Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Sorting
SortingSorting
Sorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 

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

Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmDeepika Walanjkar
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxStephen Sitton
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming languageSmritiSharma901052
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodManicka Mamallan Andavar
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 

Recently uploaded (20)

Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 

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