SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp AlgorithmSohail Ahmed
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 
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 applicationsyazad dumasia
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptracha49
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 

What's hot (20)

5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Recursion
RecursionRecursion
Recursion
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Merge sort
Merge sortMerge sort
Merge sort
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Quick sort
Quick sortQuick sort
Quick sort
 
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
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 

Viewers also liked

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
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
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
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
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
 

Viewers also liked (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 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
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
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
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Selection sort
Selection sortSelection sort
Selection sort
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
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
 

Similar to Mergesort

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
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
 

Similar to 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
 

More from 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
 

More from 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
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
🐬 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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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...
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

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.