SlideShare a Scribd company logo
3
Most read
4
Most read
7
Most read
 Merge Sort
Merge Sort
 Divide and Conquer
 Recursive in structure
 Divide the problem into sub-problems that are similar
to the original but smaller in size
 Conquer the sub-problems by solving them recursively.
If they are small enough, just solve them in a
straightforward manner.
 Combine the solutions to create a solution to the
original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.
 Divide: Divide the n-element sequence to be
sorted into two subsequences of n/2 elements each
 Conquer: Sort the two subsequences recursively
using merge sort.
 Combine: Merge the two sorted subsequences to
produce the sorted answer.
Merge Sort – Example
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
2618 632 1543 19
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 326 15 43 1 9
6 18 26 32 1 9 15 43
1 6 9 15 18 26 32 43
18 26
18 26
18 26
32
32
6
6
32 6
18 26 32 6
43
43
15
15
43 15
9
9
1
1
9 1
43 15 9 1
18 26 32 6 43 15 9 1
18 26 632
626 3218
1543 19
1 915 43
16 9 1518 26 32 43
Original Sequence Sorted Sequence
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
MergeSort (A, p, r) // sort A[p..r] by divide & conquer
1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]
Initial Call: MergeSort(A, 1, n)
Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q
3 for i  1 to n1
4 do L[i]  A[p + i – 1]
5 for j  1 to n2
6 do R[j]  A[q + j]
7 L[n1+1]  
8 R[n2+1]  
9 i  1
10 j  1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 i  i + 1
15 else A[k]  R[j]
16 j  j + 1
Input: Array containing sorted subarrays
A[p..q] and A[q+1..r].
Output: Merged sorted subarray in A[p..r].
j
Merge – Example
6 8 26 32 1 9 42 43… …A
k
6 8 26 32 1 9 42 43
k k k k k k k
i i i i
 
i j j j j
6 8 26 32 1 9 42 43
1 6 8 9 26 32 42 43
k
L R
Analysis of Merge Sort
 Running time T(n) of Merge Sort:
 Divide: computing the middle takes (1)
 Conquer: solving 2 sub-problems takes 2T(n/2)
 Combine: merging n elements takes (n)
 Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
T(n) = 2 T(n/2) + n
= 2 ((n/2)log(n/2) + (n/2)) + n
= n (log(n/2)) + 2n
= n log n – n + 2n
= n log n + n
= O(n log n )
Comparing the Algorithms
Best Average Worst
Case Case Case
Bubble Sort O(n) O(n2) O(n2)
Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
Heap Sort O(n log n) O(n log n) O(n log n)
Thank You

More Related Content

PPT
Merge sort
PPTX
Insertion sort
PPT
Medians and order statistics
PPTX
Merge sort algorithm power point presentation
PPTX
Merge Sort
PPT
Quick sort Algorithm Discussion And Analysis
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Merge sort algorithm
Merge sort
Insertion sort
Medians and order statistics
Merge sort algorithm power point presentation
Merge Sort
Quick sort Algorithm Discussion And Analysis
Binary Search - Design & Analysis of Algorithms
Merge sort algorithm

What's hot (20)

PPTX
Insertion sort algorithm power point presentation
PPTX
Merge sort
PDF
linear search and binary search
PDF
Algorithms Lecture 5: Sorting Algorithms II
PPTX
Sorting algorithms
PPTX
Merge sort and quick sort
PPTX
Radix sort presentation
PPTX
PPTX
Breadth First Search & Depth First Search
PPTX
Searching
PPTX
single linked list
PPTX
Searching techniques
PDF
Algorithms Lecture 4: Sorting Algorithms I
PDF
sparse matrix in data structure
PPTX
Selection sort 1
PPT
Divide and conquer
PPTX
Queue - Data Structure - Notes
PDF
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
PPT
Red black tree
PPTX
Brute force method
Insertion sort algorithm power point presentation
Merge sort
linear search and binary search
Algorithms Lecture 5: Sorting Algorithms II
Sorting algorithms
Merge sort and quick sort
Radix sort presentation
Breadth First Search & Depth First Search
Searching
single linked list
Searching techniques
Algorithms Lecture 4: Sorting Algorithms I
sparse matrix in data structure
Selection sort 1
Divide and conquer
Queue - Data Structure - Notes
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
Red black tree
Brute force method
Ad

Similar to Data Structure and Algorithms Merge Sort (20)

PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PPT
MergesortQuickSort.ppt
PPT
Admission in india 2015
PPT
Divide and conquer
PPT
03 dc
PPT
5.2 divide and conquer
PDF
PPT
3-Chapter Three - Divide and Conquer.ppt
PPTX
quick and merge.pptx
PDF
module2_dIVIDEncONQUER_2022.pdf
PPTX
L2_DatabAlgorithm Basics with Design & Analysis.pptx
PPTX
Data Structure and algorithms for software
PPT
Mergesort
PPTX
Daa final
PPT
5.2 divede and conquer 03
PPT
5.2 divede and conquer 03
PPT
Unit 7 sorting
PPT
Divide and conquer
PPT
5.5 back tracking 02
presentation_mergesortquicksort_1458716068_193111.ppt
MergesortQuickSort.ppt
Admission in india 2015
Divide and conquer
03 dc
5.2 divide and conquer
3-Chapter Three - Divide and Conquer.ppt
quick and merge.pptx
module2_dIVIDEncONQUER_2022.pdf
L2_DatabAlgorithm Basics with Design & Analysis.pptx
Data Structure and algorithms for software
Mergesort
Daa final
5.2 divede and conquer 03
5.2 divede and conquer 03
Unit 7 sorting
Divide and conquer
5.5 back tracking 02
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Search Tree
PPT
Data Structure and Algorithms Binary Tree
PPT
Data Structure and Algorithms Queues
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPT
Data Structure and Algorithms Stacks
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure and Algorithms Sorting
PPT
Data Structure and Algorithms Arrays
PPT
Data Structure and Algorithms
PPT
Data Structure and Algorithms Hashing
PPTX
Data Structure and Algorithms Graph Traversal
PPT
Data Structure and Algorithms Graphs
PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
Data Structure and Algorithms Heaps and Trees
PPT
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Queues
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms Stacks
Data Structure and Algorithms Linked List
Data Structure and Algorithms Sorting
Data Structure and Algorithms Arrays
Data Structure and Algorithms
Data Structure and Algorithms Hashing
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graphs
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms AVL Trees

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administraation Chapter 3
PDF
Digital Strategies for Manufacturing Companies
PDF
AI in Product Development-omnex systems
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
top salesforce developer skills in 2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PDF
Best Practices for Rolling Out Competency Management Software.pdf
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Introduction to Artificial Intelligence
Presentation of Computer CLASS 2 .pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
A REACT POMODORO TIMER WEB APPLICATION.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administraation Chapter 3
Digital Strategies for Manufacturing Companies
AI in Product Development-omnex systems
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Migrate SBCGlobal Email to Yahoo Easily
Odoo POS Development Services by CandidRoot Solutions
top salesforce developer skills in 2025.pdf
medical staffing services at VALiNTRY
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Best Practices for Rolling Out Competency Management Software.pdf
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes

Data Structure and Algorithms Merge Sort

  • 2. Merge Sort  Divide and Conquer  Recursive in structure  Divide the problem into sub-problems that are similar to the original but smaller in size  Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner.  Combine the solutions to create a solution to the original problem
  • 3. An Example: Merge Sort Sorting Problem: Sort a sequence of n elements into non-decreasing order.  Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each  Conquer: Sort the two subsequences recursively using merge sort.  Combine: Merge the two sorted subsequences to produce the sorted answer.
  • 4. Merge Sort – Example 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 2618 632 1543 19 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 326 15 43 1 9 6 18 26 32 1 9 15 43 1 6 9 15 18 26 32 43 18 26 18 26 18 26 32 32 6 6 32 6 18 26 32 6 43 43 15 15 43 15 9 9 1 1 9 1 43 15 9 1 18 26 32 6 43 15 9 1 18 26 632 626 3218 1543 19 1 915 43 16 9 1518 26 32 43 Original Sequence Sorted Sequence
  • 5. Merge-Sort (A, p, r) INPUT: a sequence of n numbers stored in array A OUTPUT: an ordered sequence of n numbers MergeSort (A, p, r) // sort A[p..r] by divide & conquer 1 if p < r 2 then q  (p+r)/2 3 MergeSort (A, p, q) 4 MergeSort (A, q+1, r) 5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r] Initial Call: MergeSort(A, 1, n)
  • 6. Procedure Merge Merge(A, p, q, r) 1 n1  q – p + 1 2 n2  r – q 3 for i  1 to n1 4 do L[i]  A[p + i – 1] 5 for j  1 to n2 6 do R[j]  A[q + j] 7 L[n1+1]   8 R[n2+1]   9 i  1 10 j  1 11 for k p to r 12 do if L[i]  R[j] 13 then A[k]  L[i] 14 i  i + 1 15 else A[k]  R[j] 16 j  j + 1 Input: Array containing sorted subarrays A[p..q] and A[q+1..r]. Output: Merged sorted subarray in A[p..r].
  • 7. j Merge – Example 6 8 26 32 1 9 42 43… …A k 6 8 26 32 1 9 42 43 k k k k k k k i i i i   i j j j j 6 8 26 32 1 9 42 43 1 6 8 9 26 32 42 43 k L R
  • 8. Analysis of Merge Sort  Running time T(n) of Merge Sort:  Divide: computing the middle takes (1)  Conquer: solving 2 sub-problems takes 2T(n/2)  Combine: merging n elements takes (n)  Total: T(n) = (1) if n = 1 T(n) = 2T(n/2) + (n) if n > 1 T(n) = 2 T(n/2) + n = 2 ((n/2)log(n/2) + (n/2)) + n = n (log(n/2)) + 2n = n log n – n + 2n = n log n + n = O(n log n )
  • 9. Comparing the Algorithms Best Average Worst Case Case Case Bubble Sort O(n) O(n2) O(n2) Insertion Sort O(n) O(n2) O(n2) Selection Sort O(n2) O(n2) O(n2) Merge Sort O(n log n) O(n log n) O(n log n) Quick Sort O(n log n) O(n log n) O(n2) Heap Sort O(n log n) O(n log n) O(n log n)