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
 
Selection sort
Selection sortSelection sort
Selection sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Quick sort
Quick sortQuick sort
Quick sort
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

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

A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 

Recently uploaded (20)

A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 

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