SlideShare a Scribd company logo
1 of 22
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

Similar to MergeSort.ppt shows how merge sort is done

Similar to MergeSort.ppt shows how merge sort is done (20)

Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
 
Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
sorting
sortingsorting
sorting
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Introduction
IntroductionIntroduction
Introduction
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
n-squared_sorts
n-squared_sortsn-squared_sorts
n-squared_sorts
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
220exercises2
220exercises2220exercises2
220exercises2
 

More from SadiaSharmin40

16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
SadiaSharmin40
 
huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppt
SadiaSharmin40
 
HuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman codeHuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman code
SadiaSharmin40
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
SadiaSharmin40
 

More from SadiaSharmin40 (8)

16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithm
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algo
 
huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppt
 
HuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman codeHuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman code
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 
how to use counting sort algorithm to sort array
how to use counting sort algorithm to sort arrayhow to use counting sort algorithm to sort array
how to use counting sort algorithm to sort array
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdf
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Recently uploaded (20)

NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
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
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 

MergeSort.ppt shows how merge sort is done

  • 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.