SlideShare a Scribd company logo
1
Merge Sort
• Review of Sorting
• Merge Sort
2
Sorting Algorithms
• Selection Sort uses a priority queue P implemented
with an unsorted sequence:
– Phase 1: the insertion of an item into P takes O(1) time;
overall O(n) time for inserting n items.
– Phase 2: removing an item takes time proportional to the
number of elements in P, which is O(n): overall O(n2
)
– Time Complexity: O(n2
)
3
Sorting Algorithms (cont.)
• Insertion Sort is performed on a priority queue P which is a
sorted sequence:
– Phase 1: the first insertItem takes O(1), the second O(2), until the
last insertItem takes O(n): overall O(n2
)
– Phase 2: removing an item takes O(1) time; overall O(n).
– Time Complexity: O(n2
)
• Heap Sort uses a priority queue K which is a heap.
– insertItem and removeMin each take O(logk), k being the
number of elements in the heap at a given time.
– Phase 1: n elements inserted: O (nlogn) time
– Phase 2: n elements removed: O (nlogn) time.
– Time Complexity: O (nlog n)
4
Divide-and-Conquer
• Divide and Conquer is more than just a military strategy; it is also a
method of algorithm design that has created such efficient algorithms
as Merge Sort.
• In terms or algorithms, this method has three distinct steps:
– Divide: If the input size is too large to deal with in a straightforward manner,
divide the data into two or more disjoint subsets.
– Recur: Use divide and conquer to solve the subproblems associated with the
data subsets.
– Conquer: Take the solutions to the subproblems and “merge” these solutions
into a solution for the original problem.
5
Merge-Sort
• Algorithm:
– Divide: If S has at leas two elements (nothing needs to be done if S has zero or
one elements), remove all the elements from S and put them into two sequences,
S1 and S2, each containing about half of the elements of S. (i.e. S1 contains the
first n/2 elements and S2 contains the remaining n/2 elements.
– Recur: Recursive sort sequences S1 and S2.
– Conquer: Put back the elements into S by merging the sorted sequences S1 and
S2 into a unique sorted sequence.
• Merge Sort Tree:
– Take a binary tree T
– Each node of T represents a recursive call of the merge sort algorithm.
– We associate with each node v of T a the set of input passed to the invocation v
represents.
– The external nodes are associated with individual elements of S, upon which no
recursion is called.
6
Merge-Sort
7
Merge-Sort(cont.)
8
Merge-Sort (cont.)
9
Merge-Sort (cont.)
10
Merge-Sort (cont.)
11
Merge-Sort (cont.)
12
Merge-Sort (cont.)
13
Merge-Sort(cont.)
14
Merge-Sort (cont.)
15
Merge-Sort (cont.)
16
Merge-Sort (cont.)
17
Merging Two Sequences
18
Merging Two Sequences (cont.)
19
Merging Two Sequences (cont.)
20
Merging Two Sequences (cont.)
21
Merging Two Sequences (cont.)
22
Merging Two Sequences (cont.)

More Related Content

PPT
MergeSort.ppt shows how merge sort is done
PPTX
Merge Sort with real example using animation
PPT
Insert Sort & Merge Sort Using C Programming
PPTX
Data structure Merge Sort implementation
PDF
Binary Sort
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
PPT
Algorithms and Data structures: Merge Sort
MergeSort.ppt shows how merge sort is done
Merge Sort with real example using animation
Insert Sort & Merge Sort Using C Programming
Data structure Merge Sort implementation
Binary Sort
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
Algorithms and Data structures: Merge Sort

Similar to MergeSort.ppt algorithm History and Examples (20)

PPTX
Merge sort analysis and its real time applications
PPT
Quicksort
PPTX
DSA- Merge Sort-a sorting technique.pptx
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
PPT
Implication of Merge sort (Artificial Intelligence).ppt
PDF
Algorithms lecture 3
PDF
Analysis and design of algorithms part2
PPTX
Merge sort algorithm
PPTX
Merge sort and quick sort
PPT
PPTX
Module 2_ Divide and Conquer Approach.pptx
PPTX
Lecture -16-merge sort (slides).pptx
PDF
Study on Sorting Algorithm and Position Determining Sort
PPT
Advanced s and s algorithm.ppt
PPT
MergesortQuickSort.ppt
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PPTX
Mergesort
PPTX
Data Structure Marge sort Group 5 pptx so
PPTX
sorting-160810203705.pptx
PPTX
All Searching and Sorting Techniques in Data Structures
Merge sort analysis and its real time applications
Quicksort
DSA- Merge Sort-a sorting technique.pptx
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
Implication of Merge sort (Artificial Intelligence).ppt
Algorithms lecture 3
Analysis and design of algorithms part2
Merge sort algorithm
Merge sort and quick sort
Module 2_ Divide and Conquer Approach.pptx
Lecture -16-merge sort (slides).pptx
Study on Sorting Algorithm and Position Determining Sort
Advanced s and s algorithm.ppt
MergesortQuickSort.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
Mergesort
Data Structure Marge sort Group 5 pptx so
sorting-160810203705.pptx
All Searching and Sorting Techniques in Data Structures
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
DOCX
573137875-Attendance-Management-System-original
PDF
PPT on Performance Review to get promotions
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
web development for engineering and engineering
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
573137875-Attendance-Management-System-original
PPT on Performance Review to get promotions
R24 SURVEYING LAB MANUAL for civil enggi
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
UNIT 4 Total Quality Management .pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
web development for engineering and engineering
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
Digital Logic Computer Design lecture notes
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Ad

MergeSort.ppt algorithm History and Examples

  • 1. 1 Merge Sort • Review of Sorting • Merge Sort
  • 2. 2 Sorting Algorithms • Selection Sort uses a priority queue P implemented with an unsorted sequence: – Phase 1: the insertion of an item into P takes O(1) time; overall O(n) time for inserting n items. – Phase 2: removing an item takes time proportional to the number of elements in P, which is O(n): overall O(n2 ) – Time Complexity: O(n2 )
  • 3. 3 Sorting Algorithms (cont.) • Insertion Sort is performed on a priority queue P which is a sorted sequence: – Phase 1: the first insertItem takes O(1), the second O(2), until the last insertItem takes O(n): overall O(n2 ) – Phase 2: removing an item takes O(1) time; overall O(n). – Time Complexity: O(n2 ) • Heap Sort uses a priority queue K which is a heap. – insertItem and removeMin each take O(logk), k being the number of elements in the heap at a given time. – Phase 1: n elements inserted: O (nlogn) time – Phase 2: n elements removed: O (nlogn) time. – Time Complexity: O (nlog n)
  • 4. 4 Divide-and-Conquer • Divide and Conquer is more than just a military strategy; it is also a method of algorithm design that has created such efficient algorithms as Merge Sort. • In terms or algorithms, this method has three distinct steps: – Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or more disjoint subsets. – Recur: Use divide and conquer to solve the subproblems associated with the data subsets. – Conquer: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem.
  • 5. 5 Merge-Sort • Algorithm: – Divide: If S has at leas two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S1 and S2, each containing about half of the elements of S. (i.e. S1 contains the first n/2 elements and S2 contains the remaining n/2 elements. – Recur: Recursive sort sequences S1 and S2. – Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a unique sorted sequence. • Merge Sort Tree: – Take a binary tree T – Each node of T represents a recursive call of the merge sort algorithm. – We associate with each node v of T a the set of input passed to the invocation v represents. – The external nodes are associated with individual elements of S, upon which no recursion is called.