SlideShare a Scribd company logo
Merge Sort
Chapter 2
Merge Sort 2
Divide-and-Conquer
• Divide-and conquer is a
general algorithm design
paradigm:
• Divide: divide the input
data S in two disjoint
subsets S1 and S2
• Recur: solve the
subproblems associated
with S1 and S2
• Conquer: combine the
solutions for S1 and S2 into
a solution for S
• The base case for the
recursion are subproblems
of size 0 or 1
Merge Sort 3
Merge-Sort
• Merge-sort is a sorting algorithm based on the divide-and-
conquer paradigm
• Like heap-sort
• It has O(n log n) running time
• Unlike heap-sort
• It does not use an auxiliary priority queue
• It accesses data in a sequential manner (suitable to sort data on a
disk)
Merge Sort 4
The Merge-Sort Algorithm
• Merge-sort on an input
sequence S with n
elements consists of
three steps:
• Divide: partition S into
two sequences S1 and S2
of about n/2 elements
each
• Recur: recursively sort S1
and S2
• Conquer: merge S1 and
S2 into a unique sorted
sequence
Algorithm mergeSort(S)
Input sequence S with n
elements
Output sequence S sorted
according to C
if S.size() > 1
(S1, S2)  partition(S,
n/2)
mergeSort(S1)
mergeSort(S2)
S  merge(S1, S2)
Algorithm
void mergeSort(vector<T>& arr, int left, int right) {
if (left >= right)
return;
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort with real example using animation
Merge Sort 104
Analysis of Merge-Sort
• The height h of the merge-sort tree is O(log n)
• at each recursive call we divide in half the sequence,
• The overall amount or work done at the nodes of depth i is
O(n)
• we partition and merge 2i
sequences of size n/2i
• we make 2i+1
recursive calls
• Thus, the total running time of merge-sort is O(n log n)
depth #seqs size
0 1 n
1 2 n/2
i 2i
n/2i
… … …
Merge Sort 105
Summary of Sorting
Algorithms
Algorithm Time Notes
selection-sort O(n2
)
 slow
 in-place
 for small data sets (< 1K)
insertion-sort O(n2
)
 slow
 in-place
 for small data sets (< 1K)
heap-sort O(n log n)
 fast
 in-place
 for large data sets (1K — 1M)
merge-sort O(n log n)
 fast
 sequential data access
 for huge data sets (> 1M)

More Related Content

PDF
Sorting
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
Counting Sort Lowerbound
PPT
Quicksort
PPT
Insertion sort bubble sort selection sort
Sorting
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
Counting Sort Lowerbound
Quicksort
Insertion sort bubble sort selection sort

Similar to Merge Sort with real example using animation (20)

PDF
Analysis and design of algorithms part2
PPT
Cis435 week06
PPTX
sorting-160810203705.pptx
PPT
InsertionSortBubbleSortSelectionSort.ppt
PPT
Quick sort Algorithm Discussion And Analysis
PPTX
1_Asymptotic_Notation_pptx.pptx
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
Data Structures 6
PPT
lecture 9
PPTX
Sorting algorithms
PDF
Class13_Quicksort_Algorithm.pdf
PPTX
Algorithms - "Chapter 2 getting started"
PPT
Introduction
PPTX
Lecture -16-merge sort (slides).pptx
PPT
Algorithm Design and Analysis
PPT
Sorting Algorithms for data struture prog
PDF
Lecture 3 insertion sort and complexity analysis
PPTX
Different Searching and Sorting Methods.pptx
PPTX
Merge sort and quick sort
Analysis and design of algorithms part2
Cis435 week06
sorting-160810203705.pptx
InsertionSortBubbleSortSelectionSort.ppt
Quick sort Algorithm Discussion And Analysis
1_Asymptotic_Notation_pptx.pptx
Sorting algorithms bubble sort to merge sort.pdf
Data Structures 6
lecture 9
Sorting algorithms
Class13_Quicksort_Algorithm.pdf
Algorithms - "Chapter 2 getting started"
Introduction
Lecture -16-merge sort (slides).pptx
Algorithm Design and Analysis
Sorting Algorithms for data struture prog
Lecture 3 insertion sort and complexity analysis
Different Searching and Sorting Methods.pptx
Merge sort and quick sort
Ad

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
top salesforce developer skills in 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
ai tools demonstartion for schools and inter college
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
history of c programming in notes for students .pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
wealthsignaloriginal-com-DS-text-... (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo POS Development Services by CandidRoot Solutions
top salesforce developer skills in 2025.pdf
Operating system designcfffgfgggggggvggggggggg
ai tools demonstartion for schools and inter college
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How Creative Agencies Leverage Project Management Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Digital Strategies for Manufacturing Companies
PTS Company Brochure 2025 (1).pdf.......
history of c programming in notes for students .pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Ad

Merge Sort with real example using animation

  • 2. Merge Sort 2 Divide-and-Conquer • Divide-and conquer is a general algorithm design paradigm: • Divide: divide the input data S in two disjoint subsets S1 and S2 • Recur: solve the subproblems associated with S1 and S2 • Conquer: combine the solutions for S1 and S2 into a solution for S • The base case for the recursion are subproblems of size 0 or 1
  • 3. Merge Sort 3 Merge-Sort • Merge-sort is a sorting algorithm based on the divide-and- conquer paradigm • Like heap-sort • It has O(n log n) running time • Unlike heap-sort • It does not use an auxiliary priority queue • It accesses data in a sequential manner (suitable to sort data on a disk)
  • 4. Merge Sort 4 The Merge-Sort Algorithm • Merge-sort on an input sequence S with n elements consists of three steps: • Divide: partition S into two sequences S1 and S2 of about n/2 elements each • Recur: recursively sort S1 and S2 • Conquer: merge S1 and S2 into a unique sorted sequence Algorithm mergeSort(S) Input sequence S with n elements Output sequence S sorted according to C if S.size() > 1 (S1, S2)  partition(S, n/2) mergeSort(S1) mergeSort(S2) S  merge(S1, S2)
  • 5. Algorithm void mergeSort(vector<T>& arr, int left, int right) { if (left >= right) return; int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); }
  • 104. Merge Sort 104 Analysis of Merge-Sort • The height h of the merge-sort tree is O(log n) • at each recursive call we divide in half the sequence, • The overall amount or work done at the nodes of depth i is O(n) • we partition and merge 2i sequences of size n/2i • we make 2i+1 recursive calls • Thus, the total running time of merge-sort is O(n log n) depth #seqs size 0 1 n 1 2 n/2 i 2i n/2i … … …
  • 105. Merge Sort 105 Summary of Sorting Algorithms Algorithm Time Notes selection-sort O(n2 )  slow  in-place  for small data sets (< 1K) insertion-sort O(n2 )  slow  in-place  for small data sets (< 1K) heap-sort O(n log n)  fast  in-place  for large data sets (1K — 1M) merge-sort O(n log n)  fast  sequential data access  for huge data sets (> 1M)