SlideShare a Scribd company logo
Presentacion tomada del Depto. De  Si stemas de la Universidad Nacional de Colombia se de  Bogota Analysis of Algorithms
Merge-Sort INPUT:  A sequence of numbers <a 1 ,a 2 ,a 3 ,...,a n > DIVIDE AND CONQUER PARADIGM Divide:   the problem into a number of sub-problems Conquer:   the sub-problems by solving them recursively Combine:   the solutions to the sub-problems into the solution for the original problem
MERGE-SORT DIVIDE:   Divide the n-element sequence to be sorted  into two subsequences of n/2 elements each CONQUER:   sort the two subsequences recursively using merge sort COMBINE:   merge the two sorted subsequences to produce the sorted answer
STEPS: on a problem of size 1 do nothing on a problem of size at least 2 Split the sequence into two halves Sort one half of the numbers Sort the second half of the numbers Merge the two sorted lists MERGE-SORT
Given two lists to merge size n and m Maintain pointer to head of each list Move smaller element to output and advance pointer n m n+m ( First we study merge  )  MERGE
auxiliary array smallest smallest A MERGE Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done. This animation is taken form  http://www.cs.princeton.edu/courses/cs226/lectures.html A G L O R H I M S T
auxiliary array smallest smallest MERGE A G A G L O R H I M S T
auxiliary array MERGE A G H smallest smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I L smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I L M smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I L M O smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I L M O R smallest A G L O R H I M S T
auxiliary array smallest MERGE A G H I L M O R S first half exhausted A G L O R H I M S T
auxiliary array MERGE A G H I L M O R first half exhausted S T smallest A G L O R H I M S T
auxiliary array MERGE A G H I L M O R first half exhausted S T second half exhausted A G L O R H I M S T
MERGE ( A, p,q, r ) n 1   q-p+1  n 2     r-q create arrays L[1..n 1 +1] and R[1.. n 2 +1] for i   1 to n 1  do L[i]    A[p+i-1] for j   1 to n 2  do R[j]    A[q+j] L[n 1 +1]      R[n 2 +1]      i   1 j   1 for k    p to r   do if L[i]    R[j] then A[k]    L[i] i    i+1 else A[k]    R[j] j    j+1 end_if end_for end. MERGE
... 2 7 MERGE ( A, 5,8,11 ) 9 A 5 1 7 9  k 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R i j
... 2 7 9 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 k i j
... 3 9 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 2 k i j
... 5 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 2 3 k i j
... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 7 5 8 1 8 5 3  L R 1 2 3 5 k i j
... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 8 8 1 8 5 3  L R 1 2 3 5 7 k i j
... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 8 1 8 5 3  L R 1 2 3 5 7 8 k i j
... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 1 8 5 3  L R 1 2 3 5 7 8 9 k i j
MERGE- Correctness Loop Invariant At the start of each iteration of the for loop for k,  the sub-array A[p,..,k-1] consist of the k-p smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1] in sorted order. Moreover L[i] and R[j] are the smallest of their arrays that have not been copied back into A.
Before the beginning of the loop k=p,  then the sub-array A[p,..,k-1] is empty and consist of the k-p = 0 smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1]. Since i=j=1 then L[1] and R[1] are the smallest of their arrays that have not been copied back into A. INITILIZATION
MAINTENANCE Before the beginning of the l-th iteration of the loop k=p+l,  then the sub-array A[p,..,k-1]  consist of the (k-p = l) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] in sorted order and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
Let us first assume that L[i]    R[j] then following the loop L[i] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
Now suppose that R[j] < L[i] then following the loop R[j] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
At termination k=r+1,  then the sub-array A[p,..,k-1]= A[p,..,r] consist of the r smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] in sorted order. Since i= n 1   +1 and j= n 2   +1 then L[i] and R[j] are   . TERMINATION
MERGE-SORT MERGE(A, p, r): procedure that takes time   (n), where n=r-p+1 To sort call MERGE(A, 1, length[A]) with A = [ a 1 ,a 2 ,a 3 ,...,a n   ] procedure MERGE-SORT( A, p, r  ) if p<r then q      (p+r)/2     MERGE-SORT( A, p, q )   MERGE-SORT( A, q+1, r )   MERGE ( A, p, q, r )
Initial Sequence Sorted Sequence divide divide divide merge merge merge 5  2  4  6  1  3  2  6  5  2  4  6  1  3  2  6  5  2 4  6 5 2 2  5 4 6 4  6 2  4  5  6  1  3 2  6 1 3 1  3 2 6 2  6 1  2  3  6  1  2  2  3  4  5  6  6
Time complexity Analyzing divide and conquer algorithms The time can often be described by recurrence equation of the form    (1),    if n     c, T(n) =   aT(n/b)+D(n)+C(n)    if n>c With a,b and c be nonnegative constants. If the problem is the small enough, say  n     c , then the solution takes constant time   (1). If not the problem is divided in  a  subproblems with  (1/b)  size of the original. The division takes time  D(n)  and the combinations of sub-solutions takes time  C(n) .
Analyzing MERGE-SORT In this case  a=2, b=2, c=1, D(n)=  (1)  and   C(n)=    (n) then    (1),    if n   1 , T(n) =   2T(n/2)+   (1)+   (n)    if n>1   c    if n   1 , T(n) =   2T(n/2)+ cn   if n>1
Initial Sequence Sorted Sequence merge merge merge 1  2  2  3  4  5  6  6  2  4  5  6 1  2  3  6  2  5 2  6 1  3 4  6 5 2 4 6 1 3 2 6
Proof by Picture of Recursion Tree T( n ) T( n /2) T( n /2) T( n /4) T( n /4) T( n /4) T( n /4) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) cn T( n / 2 i ) c2( n /2) c4( n  /4) c2 i  ( n  / 2 i ) cn . . . . . . lg n+1 cn(1+   lg   n)
Lets suppose n power of two n=2 k The construction of the recursion tree T( n ) cn T( n  /2) T( n /2)
cn c(n/2) c(n/2) T( n  /4) T( n  /4) T( n /4) T( n /4)
cn c(n/2) c(n/2) c( n  /4) c( n  /4) c( n  /4) c( n  /4) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8)
cn c(n/2) c(n/2) c( n  /4) c( n  /4) c( n  /4) c( n  /4) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c c c c c c c c c c c c c c c c lg n+1 n Total: cn (lg n + 1) cn lg n + cn cn   cn   cn   cn   cn
k times Lets assume that  n  is power of two, i.e., n=2 k ,  k = lg n T(n)  = 2T(n/2)+ cn  = 2[2T(n/4)+ cn/2]+ cn = 4T(n/4)+ cn+ cn  = 4[2T(n/8)+cn/4]+ cn+ cn= 8T(n/8)+cn+ cn+ cn . . = 2 k T(n/2 k )+cn+ . . . +cn   .   . = 2 k T(1)+k(cn)  = cn+cn lg n Recursive substitution
In total including other operations let’s say each merge costs 3 per element output T(n)=T (  n/2  ) +T (  n/2  ) +3n  for n    2 T(1)=1 Can use this to figure out T for any value of n T(5) =T(3)+T(2)+3x5    = (T(2)+T(1)+3x3)+(T(1)+T(1)+3x2)+15  = ((T(1)+T(1)+6)+1+9)+(1+1+6)+15  = 8+10+8+15 = 41 “ ceiling” round up “ floor” round down Exact recursive recurrence

More Related Content

What's hot (20)

PPT
5.2 divide and conquer
Krish_ver2
 
PPTX
Merge Sort
Nikhil Sonkamble
 
PPTX
Presentation-Merge Sort
Md Showrov Ahmed
 
PPTX
Merge sort algorithm
srutisenpatra
 
PPT
3.6 radix sort
Krish_ver2
 
PPTX
Vertex cover Problem
Gajanand Sharma
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PDF
Quick sort algorithn
Kumar
 
PPT
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
PPTX
Bfs and Dfs
Masud Parvaze
 
PPT
Dijkstra.ppt
Ruchika Sinha
 
PPTX
Minimization of DFA.pptx
SadagopanS
 
PPTX
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
PPTX
Daa final
Gagan019
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPTX
Dijkstra
jagdeeparora86
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PPT
Merge sort
Soumen Santra
 
PPTX
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
PPTX
OPTIMAL BINARY SEARCH
Cool Guy
 
5.2 divide and conquer
Krish_ver2
 
Merge Sort
Nikhil Sonkamble
 
Presentation-Merge Sort
Md Showrov Ahmed
 
Merge sort algorithm
srutisenpatra
 
3.6 radix sort
Krish_ver2
 
Vertex cover Problem
Gajanand Sharma
 
Divide and conquer
Dr Shashikant Athawale
 
Quick sort algorithn
Kumar
 
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Bfs and Dfs
Masud Parvaze
 
Dijkstra.ppt
Ruchika Sinha
 
Minimization of DFA.pptx
SadagopanS
 
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Daa final
Gagan019
 
Merge sort algorithm
Shubham Dwivedi
 
Dijkstra
jagdeeparora86
 
Data Structures - Searching & sorting
Kaushal Shah
 
Merge sort
Soumen Santra
 
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
OPTIMAL BINARY SEARCH
Cool Guy
 

Viewers also liked (20)

PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PDF
Merge sort
Srikrishnan Suresh
 
PPTX
Merge sort
Chusnul Khotimah
 
PPTX
Merge sort code in C explained
Mohit Tare
 
PDF
Merge sort: illustrated step-by-step walk through
Yoshi Watanabe
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPT
Quick Sort
priyankanaidu6
 
PPTX
Bubble Sort
geeortiz
 
PPTX
Divide and conquer 1
Kumar
 
PDF
Bubblesort Algorithm
Tobias Straub
 
PPTX
Knapsack Problem
Jenny Galino
 
PPT
Master method
Rajendran
 
PPT
Greedy
koralverma
 
PPT
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Merge sort analysis and its real time applications
yazad dumasia
 
PPTX
Selection sort
Jay Patel
 
PDF
Divide and Conquer
Mohammed Hussein
 
PDF
x1 t10 04 maximum & minimum problems (13)
Nigel Simmons
 
PDF
Alg1 8.2 Substitution Method
Jaqueline Vallejo
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Merge sort
Srikrishnan Suresh
 
Merge sort
Chusnul Khotimah
 
Merge sort code in C explained
Mohit Tare
 
Merge sort: illustrated step-by-step walk through
Yoshi Watanabe
 
Merge sort and quick sort
Shakila Mahjabin
 
Quick Sort
priyankanaidu6
 
Bubble Sort
geeortiz
 
Divide and conquer 1
Kumar
 
Bubblesort Algorithm
Tobias Straub
 
Knapsack Problem
Jenny Galino
 
Master method
Rajendran
 
Greedy
koralverma
 
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Dinive conquer algorithm
Mohd Arif
 
Merge sort analysis and its real time applications
yazad dumasia
 
Selection sort
Jay Patel
 
Divide and Conquer
Mohammed Hussein
 
x1 t10 04 maximum & minimum problems (13)
Nigel Simmons
 
Alg1 8.2 Substitution Method
Jaqueline Vallejo
 
Ad

Similar to Mergesort (20)

PPT
Algorithm.ppt
Tareq Hasan
 
PPT
Divide and conquer
Vikas Sharma
 
PPTX
L2_DatabAlgorithm Basics with Design & Analysis.pptx
dpdiyakhan
 
PPT
03 dc
Hira Gul
 
PPTX
Algorithim lec1.pptx
rediet43
 
PPT
Admission in india 2015
Edhole.com
 
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
PPT
Algorithms and Data structures: Merge Sort
pharmaci
 
PDF
Sienna 4 divideandconquer
chidabdu
 
PPTX
ch16.pptx
lordaragorn2
 
PPTX
ch16 (1).pptx
lordaragorn2
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PDF
Merge Sort
Juan Zamora, MSc. MBA
 
PDF
Ada notes
VIKAS SINGH BHADOURIA
 
PPTX
Data Structure and algorithms for software
ManishShukla712917
 
PPT
1-Chapter One - Analysis of Algorithms.ppt
mershaabdisa
 
PPT
course information of design analysis of alg
Bobby Pra A
 
Algorithm.ppt
Tareq Hasan
 
Divide and conquer
Vikas Sharma
 
L2_DatabAlgorithm Basics with Design & Analysis.pptx
dpdiyakhan
 
03 dc
Hira Gul
 
Algorithim lec1.pptx
rediet43
 
Admission in india 2015
Edhole.com
 
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
Algorithms and Data structures: Merge Sort
pharmaci
 
Sienna 4 divideandconquer
chidabdu
 
ch16.pptx
lordaragorn2
 
ch16 (1).pptx
lordaragorn2
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
AliAhmad38278
 
Data Structure and algorithms for software
ManishShukla712917
 
1-Chapter One - Analysis of Algorithms.ppt
mershaabdisa
 
course information of design analysis of alg
Bobby Pra A
 
Ad

More from luzenith_g (20)

PPTX
Formacion y crecimiento
luzenith_g
 
PPTX
Formacion y crecimiento
luzenith_g
 
PPTX
Carácterísticas técnicas de las wikis
luzenith_g
 
PPTX
Web 2 0
luzenith_g
 
PPT
Alg1
luzenith_g
 
DOCX
Ejercicios Ada
luzenith_g
 
DOCX
Ejercicios Ada
luzenith_g
 
DOCX
Proyecto Unal2009 2
luzenith_g
 
DOC
Taller3 Programacion Ii
luzenith_g
 
PPT
Algoritmos Voraces (Greedy)
luzenith_g
 
PPT
Algoritmos Greedy
luzenith_g
 
PPT
Resumen
luzenith_g
 
PPT
Clase3 Notacion
luzenith_g
 
PPT
Analisis Clase2
luzenith_g
 
PPT
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
DOC
Como construir un DSS
luzenith_g
 
PPT
State Space Search(2)
luzenith_g
 
PPT
DSS:Conceptos, metodologias y Tecnologias
luzenith_g
 
PPT
Soporte a las Decisiones Computarizado
luzenith_g
 
PPT
Decision Support Systems
luzenith_g
 
Formacion y crecimiento
luzenith_g
 
Formacion y crecimiento
luzenith_g
 
Carácterísticas técnicas de las wikis
luzenith_g
 
Web 2 0
luzenith_g
 
Ejercicios Ada
luzenith_g
 
Ejercicios Ada
luzenith_g
 
Proyecto Unal2009 2
luzenith_g
 
Taller3 Programacion Ii
luzenith_g
 
Algoritmos Voraces (Greedy)
luzenith_g
 
Algoritmos Greedy
luzenith_g
 
Resumen
luzenith_g
 
Clase3 Notacion
luzenith_g
 
Analisis Clase2
luzenith_g
 
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Como construir un DSS
luzenith_g
 
State Space Search(2)
luzenith_g
 
DSS:Conceptos, metodologias y Tecnologias
luzenith_g
 
Soporte a las Decisiones Computarizado
luzenith_g
 
Decision Support Systems
luzenith_g
 

Recently uploaded (20)

PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Market Insight : ETH Dominance Returns
CIFDAQ
 

Mergesort

  • 1. Presentacion tomada del Depto. De Si stemas de la Universidad Nacional de Colombia se de Bogota Analysis of Algorithms
  • 2. Merge-Sort INPUT: A sequence of numbers <a 1 ,a 2 ,a 3 ,...,a n > DIVIDE AND CONQUER PARADIGM Divide: the problem into a number of sub-problems Conquer: the sub-problems by solving them recursively Combine: the solutions to the sub-problems into the solution for the original problem
  • 3. MERGE-SORT DIVIDE: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each CONQUER: sort the two subsequences recursively using merge sort COMBINE: merge the two sorted subsequences to produce the sorted answer
  • 4. STEPS: on a problem of size 1 do nothing on a problem of size at least 2 Split the sequence into two halves Sort one half of the numbers Sort the second half of the numbers Merge the two sorted lists MERGE-SORT
  • 5. Given two lists to merge size n and m Maintain pointer to head of each list Move smaller element to output and advance pointer n m n+m ( First we study merge ) MERGE
  • 6. auxiliary array smallest smallest A MERGE Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done. This animation is taken form http://www.cs.princeton.edu/courses/cs226/lectures.html A G L O R H I M S T
  • 7. auxiliary array smallest smallest MERGE A G A G L O R H I M S T
  • 8. auxiliary array MERGE A G H smallest smallest A G L O R H I M S T
  • 9. auxiliary array smallest MERGE A G H I smallest A G L O R H I M S T
  • 10. auxiliary array smallest MERGE A G H I L smallest A G L O R H I M S T
  • 11. auxiliary array smallest MERGE A G H I L M smallest A G L O R H I M S T
  • 12. auxiliary array smallest MERGE A G H I L M O smallest A G L O R H I M S T
  • 13. auxiliary array smallest MERGE A G H I L M O R smallest A G L O R H I M S T
  • 14. auxiliary array smallest MERGE A G H I L M O R S first half exhausted A G L O R H I M S T
  • 15. auxiliary array MERGE A G H I L M O R first half exhausted S T smallest A G L O R H I M S T
  • 16. auxiliary array MERGE A G H I L M O R first half exhausted S T second half exhausted A G L O R H I M S T
  • 17. MERGE ( A, p,q, r ) n 1  q-p+1 n 2  r-q create arrays L[1..n 1 +1] and R[1.. n 2 +1] for i  1 to n 1 do L[i]  A[p+i-1] for j  1 to n 2 do R[j]  A[q+j] L[n 1 +1]   R[n 2 +1]   i  1 j  1 for k  p to r do if L[i]  R[j] then A[k]  L[i] i  i+1 else A[k]  R[j] j  j+1 end_if end_for end. MERGE
  • 18. ... 2 7 MERGE ( A, 5,8,11 ) 9 A 5 1 7 9  k 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R i j
  • 19. ... 2 7 9 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 k i j
  • 20. ... 3 9 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 2 k i j
  • 21. ... 5 A 5 7 9  2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3  L R 1 2 3 k i j
  • 22. ... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 7 5 8 1 8 5 3  L R 1 2 3 5 k i j
  • 23. ... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 8 8 1 8 5 3  L R 1 2 3 5 7 k i j
  • 24. ... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 8 1 8 5 3  L R 1 2 3 5 7 8 k i j
  • 25. ... A 5 7 9  2 6 4 7 8 ... 9 10 11 12 1 8 5 3  L R 1 2 3 5 7 8 9 k i j
  • 26. MERGE- Correctness Loop Invariant At the start of each iteration of the for loop for k, the sub-array A[p,..,k-1] consist of the k-p smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1] in sorted order. Moreover L[i] and R[j] are the smallest of their arrays that have not been copied back into A.
  • 27. Before the beginning of the loop k=p, then the sub-array A[p,..,k-1] is empty and consist of the k-p = 0 smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1]. Since i=j=1 then L[1] and R[1] are the smallest of their arrays that have not been copied back into A. INITILIZATION
  • 28. MAINTENANCE Before the beginning of the l-th iteration of the loop k=p+l, then the sub-array A[p,..,k-1] consist of the (k-p = l) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] in sorted order and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
  • 29. Let us first assume that L[i]  R[j] then following the loop L[i] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
  • 30. Now suppose that R[j] < L[i] then following the loop R[j] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
  • 31. At termination k=r+1, then the sub-array A[p,..,k-1]= A[p,..,r] consist of the r smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] in sorted order. Since i= n 1 +1 and j= n 2 +1 then L[i] and R[j] are  . TERMINATION
  • 32. MERGE-SORT MERGE(A, p, r): procedure that takes time  (n), where n=r-p+1 To sort call MERGE(A, 1, length[A]) with A = [ a 1 ,a 2 ,a 3 ,...,a n ] procedure MERGE-SORT( A, p, r ) if p<r then q   (p+r)/2  MERGE-SORT( A, p, q ) MERGE-SORT( A, q+1, r ) MERGE ( A, p, q, r )
  • 33. Initial Sequence Sorted Sequence divide divide divide merge merge merge 5 2 4 6 1 3 2 6 5 2 4 6 1 3 2 6 5 2 4 6 5 2 2 5 4 6 4 6 2 4 5 6 1 3 2 6 1 3 1 3 2 6 2 6 1 2 3 6 1 2 2 3 4 5 6 6
  • 34. Time complexity Analyzing divide and conquer algorithms The time can often be described by recurrence equation of the form  (1), if n  c, T(n) = aT(n/b)+D(n)+C(n) if n>c With a,b and c be nonnegative constants. If the problem is the small enough, say n  c , then the solution takes constant time  (1). If not the problem is divided in a subproblems with (1/b) size of the original. The division takes time D(n) and the combinations of sub-solutions takes time C(n) .
  • 35. Analyzing MERGE-SORT In this case a=2, b=2, c=1, D(n)=  (1) and C(n)=  (n) then  (1), if n  1 , T(n) = 2T(n/2)+  (1)+  (n) if n>1 c if n  1 , T(n) = 2T(n/2)+ cn if n>1
  • 36. Initial Sequence Sorted Sequence merge merge merge 1 2 2 3 4 5 6 6 2 4 5 6 1 2 3 6 2 5 2 6 1 3 4 6 5 2 4 6 1 3 2 6
  • 37. Proof by Picture of Recursion Tree T( n ) T( n /2) T( n /2) T( n /4) T( n /4) T( n /4) T( n /4) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) cn T( n / 2 i ) c2( n /2) c4( n /4) c2 i ( n / 2 i ) cn . . . . . . lg n+1 cn(1+ lg n)
  • 38. Lets suppose n power of two n=2 k The construction of the recursion tree T( n ) cn T( n /2) T( n /2)
  • 39. cn c(n/2) c(n/2) T( n /4) T( n /4) T( n /4) T( n /4)
  • 40. cn c(n/2) c(n/2) c( n /4) c( n /4) c( n /4) c( n /4) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8)
  • 41. cn c(n/2) c(n/2) c( n /4) c( n /4) c( n /4) c( n /4) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c c c c c c c c c c c c c c c c lg n+1 n Total: cn (lg n + 1) cn lg n + cn cn cn cn cn cn
  • 42. k times Lets assume that n is power of two, i.e., n=2 k , k = lg n T(n) = 2T(n/2)+ cn = 2[2T(n/4)+ cn/2]+ cn = 4T(n/4)+ cn+ cn = 4[2T(n/8)+cn/4]+ cn+ cn= 8T(n/8)+cn+ cn+ cn . . = 2 k T(n/2 k )+cn+ . . . +cn . . = 2 k T(1)+k(cn) = cn+cn lg n Recursive substitution
  • 43. In total including other operations let’s say each merge costs 3 per element output T(n)=T (  n/2  ) +T (  n/2  ) +3n for n  2 T(1)=1 Can use this to figure out T for any value of n T(5) =T(3)+T(2)+3x5 = (T(2)+T(1)+3x3)+(T(1)+T(1)+3x2)+15 = ((T(1)+T(1)+6)+1+9)+(1+1+6)+15 = 8+10+8+15 = 41 “ ceiling” round up “ floor” round down Exact recursive recurrence