SlideShare a Scribd company logo
1 of 6
Topic: Merge Sort
1
What is Merge sort?
Merge sort is a sorting technique based on
divide and conquer technique. With worst-case
time complexity being O(n log n), it is one of the
most respected algorithms. Merge sort first
divides the array into equal halves and then
combines them in a sorted manner.
2
3
Recursive Merge Sort Algorithm
Mergesort(A[], T[] : integer array, left, right : integer) : {
if left < right then
mid := (left + right)/2;
Mergesort(A,T,left,mid);
Mergesort(A,T,mid+1,right);
Merge(A,T,left,right);
}
MainMergesort(A[1..n]: integer array, n : integer) : {
T[1..n]: integer array;
Mergesort[A,T,1,n];
}
4
Merging Technique
Merge(A[], T[] : integer array, left, right : integer) : {
mid, i, j, k, l, target : integer;
mid := (right + left)/2;
i := left; j := mid + 1; target := left;
while i < mid and j < right do
if A[i] < A[j] then T[target] := A[i] ; i:= i + 1;
else T[target] := A[j]; j := j + 1;
target := target + 1;
if i > mid then //left completed//
for k := left to target-1 do A[k] := T[k];
if j > right then //right completed//
k : = mid; l := right;
while k > i do A[l] := A[k]; k := k-1; l := l-1;
for k := left to target-1 do A[k] := T[k];
}
5
Merge sort Example
8 2 9 4 5 3 1 6
8 2 1 69 4 5 3
8 2 9 4 5 3 1 6
2 8 4 9 3 5 1 6
2 4 8 9 1 3 5 6
1 2 3 4 5 6 8 9
Merge
Merge
Merge
Divide
Divide
Divide
1 element
8 2 9 4 5 3 1 6
6
Properties of Merge sort
• Stable
– Make sure that left is sent to target on equal
values.
• Iterative Merge sort reduces copying.
• Merge sort is useful for sorting linked lists.
Thank you

More Related Content

What's hot (19)

Geom9point7
Geom9point7Geom9point7
Geom9point7
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Curve Fitting - Linear Algebra
Curve Fitting - Linear AlgebraCurve Fitting - Linear Algebra
Curve Fitting - Linear Algebra
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Group 1
Group 1Group 1
Group 1
 
4.4
4.44.4
4.4
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Domination Number on Balanced Signed Graphs
Domination Number on Balanced  Signed GraphsDomination Number on Balanced  Signed Graphs
Domination Number on Balanced Signed Graphs
 
Binary search
Binary searchBinary search
Binary search
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Workshop 04 Review
Workshop 04 ReviewWorkshop 04 Review
Workshop 04 Review
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
7th Math (C2) - L38--Jan3
7th Math (C2) - L38--Jan37th Math (C2) - L38--Jan3
7th Math (C2) - L38--Jan3
 
GH 2-6
GH 2-6GH 2-6
GH 2-6
 
Best guess list for cal paper foundation
Best guess list for cal paper foundationBest guess list for cal paper foundation
Best guess list for cal paper foundation
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Drawings, Tables, Graphs
Drawings, Tables, GraphsDrawings, Tables, Graphs
Drawings, Tables, Graphs
 

Similar to Merge sort

DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxFazlullah28
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxsgrishma559
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotCiaraAbila
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
2010990716_Tamanna Asija.pptx
2010990716_Tamanna Asija.pptx2010990716_Tamanna Asija.pptx
2010990716_Tamanna Asija.pptxPrathamGarg34
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptxworkwithiec
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 

Similar to Merge sort (20)

DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Sorting2
Sorting2Sorting2
Sorting2
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing Pivot
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
sorting
sortingsorting
sorting
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
2010990716_Tamanna Asija.pptx
2010990716_Tamanna Asija.pptx2010990716_Tamanna Asija.pptx
2010990716_Tamanna Asija.pptx
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptx
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting
SortingSorting
Sorting
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 

More from Arafat Hossan

Data Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignData Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignArafat Hossan
 
Digital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionDigital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionArafat Hossan
 
Error and Exception Handling in PHP
Error and Exception Handling in PHPError and Exception Handling in PHP
Error and Exception Handling in PHPArafat Hossan
 
Bus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorBus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorArafat Hossan
 
Mapping cardinalities
Mapping cardinalitiesMapping cardinalities
Mapping cardinalitiesArafat Hossan
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMSArafat Hossan
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating SystemArafat Hossan
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest pathArafat Hossan
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 

More from Arafat Hossan (20)

Data Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignData Dictionary in System Analysis and Design
Data Dictionary in System Analysis and Design
 
Digital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionDigital Data to Digital Signal Conversion
Digital Data to Digital Signal Conversion
 
Error and Exception Handling in PHP
Error and Exception Handling in PHPError and Exception Handling in PHP
Error and Exception Handling in PHP
 
Bus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorBus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 Microprocessor
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Deadlock
DeadlockDeadlock
Deadlock
 
Data model
Data modelData model
Data model
 
Mapping cardinalities
Mapping cardinalitiesMapping cardinalities
Mapping cardinalities
 
Sql in dbms
Sql in dbmsSql in dbms
Sql in dbms
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMS
 
Divisible rules
Divisible rulesDivisible rules
Divisible rules
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating System
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Recently uploaded

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 

Merge sort

  • 2. What is Merge sort? Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being O(n log n), it is one of the most respected algorithms. Merge sort first divides the array into equal halves and then combines them in a sorted manner. 2
  • 3. 3 Recursive Merge Sort Algorithm Mergesort(A[], T[] : integer array, left, right : integer) : { if left < right then mid := (left + right)/2; Mergesort(A,T,left,mid); Mergesort(A,T,mid+1,right); Merge(A,T,left,right); } MainMergesort(A[1..n]: integer array, n : integer) : { T[1..n]: integer array; Mergesort[A,T,1,n]; }
  • 4. 4 Merging Technique Merge(A[], T[] : integer array, left, right : integer) : { mid, i, j, k, l, target : integer; mid := (right + left)/2; i := left; j := mid + 1; target := left; while i < mid and j < right do if A[i] < A[j] then T[target] := A[i] ; i:= i + 1; else T[target] := A[j]; j := j + 1; target := target + 1; if i > mid then //left completed// for k := left to target-1 do A[k] := T[k]; if j > right then //right completed// k : = mid; l := right; while k > i do A[l] := A[k]; k := k-1; l := l-1; for k := left to target-1 do A[k] := T[k]; }
  • 5. 5 Merge sort Example 8 2 9 4 5 3 1 6 8 2 1 69 4 5 3 8 2 9 4 5 3 1 6 2 8 4 9 3 5 1 6 2 4 8 9 1 3 5 6 1 2 3 4 5 6 8 9 Merge Merge Merge Divide Divide Divide 1 element 8 2 9 4 5 3 1 6
  • 6. 6 Properties of Merge sort • Stable – Make sure that left is sent to target on equal values. • Iterative Merge sort reduces copying. • Merge sort is useful for sorting linked lists. Thank you