SlideShare a Scribd company logo
1 of 23
Download to read offline
4
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
5
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
midlo mid+1 hi
6
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
midlo mid+1 hi
copy to auxiliary array
aux[] E E G M R A C E R T
7
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
copy to auxiliary array
aux[] E E G M R A C E R T
i j
sorted result
8
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
9
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A
10
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A
11
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
12
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E G M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
13
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
14
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E M R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
15
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
16
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E R A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
17
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
18
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E A C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
19
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
20
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G C E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
21
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
22
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M E R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
23
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M R R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
24
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M R R Ta[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R T
i j
A C
25
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M R R Ra[]
mid
k
mid+1 hi
compare minimum in each subarray
aux[] E E G M R A C E R R
i j
A C
j
k
26
Abstract in-place merge demo
Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
E E E E E G M R R Ra[]
mid mid+1 hi
abstract in-place mergesort complete using auxiliary array
aux[] E E G M R A C E R R
A C

More Related Content

More from Yebon You (10)

(Leetcode) 328. Odd Even Linked List.pdf
(Leetcode) 328. Odd Even Linked List.pdf(Leetcode) 328. Odd Even Linked List.pdf
(Leetcode) 328. Odd Even Linked List.pdf
 
Countingsort
CountingsortCountingsort
Countingsort
 
Graph bfs
Graph bfsGraph bfs
Graph bfs
 
Graph dfs-and-path
Graph dfs-and-pathGraph dfs-and-path
Graph dfs-and-path
 
Quicksort partitioning
Quicksort partitioningQuicksort partitioning
Quicksort partitioning
 
Mergesort divide
Mergesort divideMergesort divide
Mergesort divide
 
Merge
MergeMerge
Merge
 
Bst delete
Bst deleteBst delete
Bst delete
 
Heapify down
Heapify downHeapify down
Heapify down
 
Heapify up
Heapify upHeapify up
Heapify up
 

Recently uploaded

INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
TanishkaHira1
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
dannyijwest
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
 
Compressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI ApplicationsCompressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI Applications
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 

Mergesort

  • 1. 4 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].
  • 2. 5 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] midlo mid+1 hi
  • 3. 6 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] midlo mid+1 hi copy to auxiliary array aux[] E E G M R A C E R T
  • 4. 7 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi copy to auxiliary array aux[] E E G M R A C E R T i j sorted result
  • 5. 8 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j
  • 6. 9 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A
  • 7. 10 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A
  • 8. 11 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 9. 12 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E G M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 10. 13 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 11. 14 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E M R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 12. 15 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 13. 16 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E R A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 14. 17 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 15. 18 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E A C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 16. 19 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 17. 20 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G C E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 18. 21 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 19. 22 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M E R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 20. 23 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M R R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 21. 24 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M R R Ta[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R T i j A C
  • 22. 25 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M R R Ra[] mid k mid+1 hi compare minimum in each subarray aux[] E E G M R A C E R R i j A C j k
  • 23. 26 Abstract in-place merge demo Goal: Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. E E E E E G M R R Ra[] mid mid+1 hi abstract in-place mergesort complete using auxiliary array aux[] E E G M R A C E R R A C