SlideShare ist ein Scribd-Unternehmen logo
1 von 43
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 > ,[object Object],[object Object],[object Object],[object Object]
MERGE-SORT ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],MERGE-SORT
[object Object],[object Object],[object Object],n m n+m ( First we study merge  )  MERGE
auxiliary array smallest smallest A MERGE ,[object Object],[object Object],[object Object],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
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],“ ceiling” round up “ floor” round down Exact recursive recurrence

Weitere ähnliche Inhalte

Was ist angesagt?

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
Kumar
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 

Was ist angesagt? (20)

5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Merge sort
Merge sortMerge sort
Merge sort
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Merge sort
Merge sortMerge sort
Merge sort
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Andere mochten auch

Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
Kumar
 
Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
 
x1 t10 04 maximum & minimum problems (13)
x1 t10 04 maximum & minimum problems (13)x1 t10 04 maximum & minimum problems (13)
x1 t10 04 maximum & minimum problems (13)
Nigel Simmons
 
Alg1 8.2 Substitution Method
Alg1 8.2 Substitution MethodAlg1 8.2 Substitution Method
Alg1 8.2 Substitution Method
Jaqueline Vallejo
 

Andere mochten auch (20)

Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Master method
Master method Master method
Master method
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
 
Greedy
GreedyGreedy
Greedy
 
Selection sort
Selection sortSelection sort
Selection sort
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
x1 t10 04 maximum & minimum problems (13)
x1 t10 04 maximum & minimum problems (13)x1 t10 04 maximum & minimum problems (13)
x1 t10 04 maximum & minimum problems (13)
 
Alg1 8.2 Substitution Method
Alg1 8.2 Substitution MethodAlg1 8.2 Substitution Method
Alg1 8.2 Substitution Method
 

Ähnlich wie Mergesort

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
 
lecture 1
lecture 1lecture 1
lecture 1
sajinsc
 
lecture 3
lecture 3lecture 3
lecture 3
sajinsc
 

Ähnlich wie Mergesort (20)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
03 dc
03 dc03 dc
03 dc
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
2.pptx
2.pptx2.pptx
2.pptx
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
lecture 15
lecture 15lecture 15
lecture 15
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
lecture 1
lecture 1lecture 1
lecture 1
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
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
 
lecture 3
lecture 3lecture 3
lecture 3
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 

Mehr von luzenith_g

Carácterísticas técnicas de las wikis
Carácterísticas técnicas de las wikisCarácterísticas técnicas de las wikis
Carácterísticas técnicas de las wikis
luzenith_g
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Ada
luzenith_g
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Ada
luzenith_g
 
Proyecto Unal2009 2
Proyecto Unal2009 2Proyecto Unal2009 2
Proyecto Unal2009 2
luzenith_g
 
Taller3 Programacion Ii
Taller3 Programacion IiTaller3 Programacion Ii
Taller3 Programacion Ii
luzenith_g
 
Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)
luzenith_g
 
Algoritmos Greedy
Algoritmos GreedyAlgoritmos Greedy
Algoritmos Greedy
luzenith_g
 
DSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y TecnologiasDSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y Tecnologias
luzenith_g
 

Mehr von luzenith_g (20)

Formacion y crecimiento
Formacion y crecimientoFormacion y crecimiento
Formacion y crecimiento
 
Formacion y crecimiento
Formacion y crecimientoFormacion y crecimiento
Formacion y crecimiento
 
Carácterísticas técnicas de las wikis
Carácterísticas técnicas de las wikisCarácterísticas técnicas de las wikis
Carácterísticas técnicas de las wikis
 
Web 2 0
Web 2 0Web 2 0
Web 2 0
 
Alg1
Alg1Alg1
Alg1
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Ada
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Ada
 
Proyecto Unal2009 2
Proyecto Unal2009 2Proyecto Unal2009 2
Proyecto Unal2009 2
 
Taller3 Programacion Ii
Taller3 Programacion IiTaller3 Programacion Ii
Taller3 Programacion Ii
 
Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)
 
Algoritmos Greedy
Algoritmos GreedyAlgoritmos Greedy
Algoritmos Greedy
 
Resumen
ResumenResumen
Resumen
 
Clase3 Notacion
Clase3 NotacionClase3 Notacion
Clase3 Notacion
 
Analisis Clase2
Analisis  Clase2Analisis  Clase2
Analisis Clase2
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Como construir un DSS
Como construir un DSSComo construir un DSS
Como construir un DSS
 
State Space Search(2)
State Space Search(2)State Space Search(2)
State Space Search(2)
 
DSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y TecnologiasDSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y Tecnologias
 
Soporte a las Decisiones Computarizado
Soporte a las Decisiones ComputarizadoSoporte a las Decisiones Computarizado
Soporte a las Decisiones Computarizado
 
Decision Support Systems
Decision Support SystemsDecision Support Systems
Decision Support Systems
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Mergesort

  • 1. Presentacion tomada del Depto. De Si stemas de la Universidad Nacional de Colombia se de Bogota Analysis of Algorithms
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 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.