SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplicationMegha V
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structureSoham Nanekar
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with examplemaamir farooq
 
Boyre Moore Algorithm | Computer Science
Boyre Moore Algorithm | Computer ScienceBoyre Moore Algorithm | Computer Science
Boyre Moore Algorithm | Computer ScienceTransweb Global Inc
 

Mais procurados (20)

Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Boyre Moore Algorithm | Computer Science
Boyre Moore Algorithm | Computer ScienceBoyre Moore Algorithm | Computer Science
Boyre Moore Algorithm | Computer Science
 
Matrices - Discrete Structures
Matrices - Discrete StructuresMatrices - Discrete Structures
Matrices - Discrete Structures
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 

Destaque

Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained Mohit Tare
 
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 throughYoshi Watanabe
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort AlgorithmTobias Straub
 
Master method
Master method Master method
Master method Rajendran
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithmevil eye
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
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 MethodJaqueline Vallejo
 

Destaque (19)

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 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
 
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
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Selection sort
Selection sortSelection sort
Selection sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
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
 

Semelhante a Mergesort

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probabilitybryan111472
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dagsindhu mathi
 
lecture 15
lecture 15lecture 15
lecture 15sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika MamaMa28
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
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 3
lecture 3lecture 3
lecture 3sajinsc
 

Semelhante a 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
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
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
 

Mais de luzenith_g

Formacion y crecimiento
Formacion y crecimientoFormacion y crecimiento
Formacion y crecimientoluzenith_g
 
Formacion y crecimiento
Formacion y crecimientoFormacion y crecimiento
Formacion y crecimientoluzenith_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 wikisluzenith_g
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Adaluzenith_g
 
Ejercicios Ada
Ejercicios AdaEjercicios Ada
Ejercicios Adaluzenith_g
 
Proyecto Unal2009 2
Proyecto Unal2009 2Proyecto Unal2009 2
Proyecto Unal2009 2luzenith_g
 
Taller3 Programacion Ii
Taller3 Programacion IiTaller3 Programacion Ii
Taller3 Programacion Iiluzenith_g
 
Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)Algoritmos Voraces (Greedy)
Algoritmos Voraces (Greedy)luzenith_g
 
Algoritmos Greedy
Algoritmos GreedyAlgoritmos Greedy
Algoritmos Greedyluzenith_g
 
Clase3 Notacion
Clase3 NotacionClase3 Notacion
Clase3 Notacionluzenith_g
 
Analisis Clase2
Analisis  Clase2Analisis  Clase2
Analisis Clase2luzenith_g
 
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 algoritmosluzenith_g
 
Como construir un DSS
Como construir un DSSComo construir un DSS
Como construir un DSSluzenith_g
 
State Space Search(2)
State Space Search(2)State Space Search(2)
State Space Search(2)luzenith_g
 
DSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y TecnologiasDSS:Conceptos, metodologias y Tecnologias
DSS:Conceptos, metodologias y Tecnologiasluzenith_g
 
Soporte a las Decisiones Computarizado
Soporte a las Decisiones ComputarizadoSoporte a las Decisiones Computarizado
Soporte a las Decisiones Computarizadoluzenith_g
 
Decision Support Systems
Decision Support SystemsDecision Support Systems
Decision Support Systemsluzenith_g
 

Mais de 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
 

Último

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
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 WorkerThousandEyes
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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 FMESafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Último (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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.