SlideShare a Scribd company logo
DESIGN AND ANALYSIS
OF ALGORITHMS
Merge Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 5
O(n2) sorting algorithms
Selection sort and insertion sort are both O(n2)
O(n2) sorting is infeasible for n over 100000
A different strategy?
Divide array in two equal parts
Separately sort left and right half
Combine the two sorted halves to get the full array
sorted
Combining sorted lists
Given two sorted lists A and B, combine into a
sorted list C
Compare first element of A and B
Move it into C
Repeat until all elements in A and B are over
Merging A and B
Merging two sorted lists
32 74 89
21 55 64
Merging two sorted lists
32 74 89
21 55 64
21
Merging two sorted lists
32 74 89
21 55 64
21 32
Merging two sorted lists
32 74 89
21 55 64
21 32 55
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74 89
Merge Sort
Sort A[0] to A[n/2-1]
Sort A[n/2] to A[n-1]
Merge sorted halves into B[0..n-1]
How do we sort the halves?
Recursively, using the same strategy!
Merge Sort
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
Merge Sort
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
Merge Sort
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
13 22 32 43 57 63 78 91
Divide and conquer
Break up problem into disjoint parts
Solve each part separately
Combine the solutions efficiently
Merging sorted lists
Combine two sorted lists A and B into C
If A is empty, copy B into C
If B is empty, copy A into C
Otherwise, compare first element of A and B and
move the smaller of the two into C
Repeat until all elements in A and B have been
moved
Merging
function Merge(A,m,B,n,C)
// Merge A[0..m-1], B[0..n-1] into C[0..m+n-1]
i = 0; j = 0; k = 0;
// Current positions in A,B,C respectively
while (k < m+n)
// Case 0: One of the two lists is empty
if (i==m) {j++; k++;}
if (j==n) {i++; k++;}
// Case 1: Move head of A into C
if (A[i] <= B[j]) { C[k] = B[j]; j++; k++;}
// Case 2: Move head of B into C
if (A[i] > B[j]) {C[k] = B[j]; j++; k++;}
Merge Sort
To sort A[0..n-1] into B[0..n-1]
If n is 1, nothing to be done
Otherwise
Sort A[0..n/2-1] into L (left)
Sort A[n/2..n-1] into R (right)
Merge L and R into B
Merge Sort
function MergeSort(A,left,right,B)
// Sort the segment A[left..right-1] into B
if (right - left == 1) // Base case
B[0] = A[left]
if (right - left > 1) // Recursive call
mid = (left+right)/2
MergeSort(A,left,mid,L)
MergeSort(A,mid,right,R)
Merge(L,mid-left,R,right-mid,B)

More Related Content

PDF
Python week4-lecture1-handout
PPTX
Mergesort
PPTX
Daa final
PPTX
Merge sort analysis and its real time applications
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
PPT
Algorithms and Data structures: Merge Sort
PPTX
Data Structure and algorithms for software
PPT
Insert Sort & Merge Sort Using C Programming
Python week4-lecture1-handout
Mergesort
Daa final
Merge sort analysis and its real time applications
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
Algorithms and Data structures: Merge Sort
Data Structure and algorithms for software
Insert Sort & Merge Sort Using C Programming

Similar to merge sort (20)

PPTX
10 merge sort
PPTX
L2_DatabAlgorithm Basics with Design & Analysis.pptx
PPTX
Lecture -16-merge sort (slides).pptx
PPTX
Sortings .pptx
PPTX
Insertion and merge sort
PPT
merge sort help in language C with algorithms
PPTX
Sorting ppt
PPT
24 Cs146 Jc Merge
PPT
PPTX
Merge sort
PPTX
Data Structure and Algorithms Merge Sort
PPT
Unit 7 sorting
PPTX
Merge sort
PPTX
MergeSort presentation dgdfgdfgdfgg.pptx
PPT
Lec 6 Divide and conquer of Data Structures & Algortihms
PPTX
Merge sort and quick sort
DOCX
Merge sort lab mannual
PPT
MergesortQuickSort.ppt
10 merge sort
L2_DatabAlgorithm Basics with Design & Analysis.pptx
Lecture -16-merge sort (slides).pptx
Sortings .pptx
Insertion and merge sort
merge sort help in language C with algorithms
Sorting ppt
24 Cs146 Jc Merge
Merge sort
Data Structure and Algorithms Merge Sort
Unit 7 sorting
Merge sort
MergeSort presentation dgdfgdfgdfgg.pptx
Lec 6 Divide and conquer of Data Structures & Algortihms
Merge sort and quick sort
Merge sort lab mannual
MergesortQuickSort.ppt
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Essential Infomation Tech presentation.pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Wondershare Filmora 15 Crack With Activation Key [2025
Essential Infomation Tech presentation.pptx
L1 - Introduction to python Backend.pptx
top salesforce developer skills in 2025.pdf
Nekopoi APK 2025 free lastest update
VVF-Customer-Presentation2025-Ver1.9.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How Creative Agencies Leverage Project Management Software.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Softaken Excel to vCard Converter Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Operating system designcfffgfgggggggvggggggggg
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction to Artificial Intelligence
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Understanding Forklifts - TECH EHS Solution
Ad

merge sort

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS Merge Sort MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan NPTEL MOOC,JAN-FEB 2015 Week 2, Module 5
  • 2. O(n2) sorting algorithms Selection sort and insertion sort are both O(n2) O(n2) sorting is infeasible for n over 100000
  • 3. A different strategy? Divide array in two equal parts Separately sort left and right half Combine the two sorted halves to get the full array sorted
  • 4. Combining sorted lists Given two sorted lists A and B, combine into a sorted list C Compare first element of A and B Move it into C Repeat until all elements in A and B are over Merging A and B
  • 5. Merging two sorted lists 32 74 89 21 55 64
  • 6. Merging two sorted lists 32 74 89 21 55 64 21
  • 7. Merging two sorted lists 32 74 89 21 55 64 21 32
  • 8. Merging two sorted lists 32 74 89 21 55 64 21 32 55
  • 9. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64
  • 10. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74
  • 11. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74 89
  • 12. Merge Sort Sort A[0] to A[n/2-1] Sort A[n/2] to A[n-1] Merge sorted halves into B[0..n-1] How do we sort the halves? Recursively, using the same strategy!
  • 13. Merge Sort 43 32 22 78 63 57 91 13
  • 14. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78
  • 15. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 16. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 17. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 18. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32
  • 19. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 20. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57
  • 21. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 22. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43
  • 23. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78
  • 24. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63
  • 25. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91
  • 26. Merge Sort 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78
  • 27. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91
  • 28. Merge Sort 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91 13 22 32 43 57 63 78 91
  • 29. Divide and conquer Break up problem into disjoint parts Solve each part separately Combine the solutions efficiently
  • 30. Merging sorted lists Combine two sorted lists A and B into C If A is empty, copy B into C If B is empty, copy A into C Otherwise, compare first element of A and B and move the smaller of the two into C Repeat until all elements in A and B have been moved
  • 31. Merging function Merge(A,m,B,n,C) // Merge A[0..m-1], B[0..n-1] into C[0..m+n-1] i = 0; j = 0; k = 0; // Current positions in A,B,C respectively while (k < m+n) // Case 0: One of the two lists is empty if (i==m) {j++; k++;} if (j==n) {i++; k++;} // Case 1: Move head of A into C if (A[i] <= B[j]) { C[k] = B[j]; j++; k++;} // Case 2: Move head of B into C if (A[i] > B[j]) {C[k] = B[j]; j++; k++;}
  • 32. Merge Sort To sort A[0..n-1] into B[0..n-1] If n is 1, nothing to be done Otherwise Sort A[0..n/2-1] into L (left) Sort A[n/2..n-1] into R (right) Merge L and R into B
  • 33. Merge Sort function MergeSort(A,left,right,B) // Sort the segment A[left..right-1] into B if (right - left == 1) // Base case B[0] = A[left] if (right - left > 1) // Recursive call mid = (left+right)/2 MergeSort(A,left,mid,L) MergeSort(A,mid,right,R) Merge(L,mid-left,R,right-mid,B)