SlideShare a Scribd company logo
1
Designing algorithms
 There are many ways to design an algorithm.
 Insertion sort uses an incremental approach: having
sorted the sub-array A[1…j - 1], we insert the single
element A[ j] into its proper place, yielding the sorted
sub-array A[1…j].
 Another approach to design is the divide-and-conquer
approach which has a recursive structure to solve a
given problem;
 they break the problem into several sub-problems that are
similar to the original problem but smaller in size,
 solve the sub-problems recursively,
 and then combine these solutions to create a solution to the
original problem.
2
The divide-and-conquer approach
 Recursive in structure
 Divide the problem into several smaller sub-
problems that are similar to the original but
smaller in size
 Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
 Combine the solutions to create a solution to the
original problem
3
An Example: Merge Sort
 Divide: Divide the n-element sequence to be
sorted into two subsequences of n/2
elements each
 Conquer: Sort the two subsequences
recursively using merge sort.
 Combine: Merge the two sorted
subsequences to produce the sorted answer.
4
Merge Sort
 To sort n numbers
 if n = 1 done!
 recursively sort 2 lists of
numbers n/2 and n/2
elements
 merge 2 sorted lists in O(n)
time
 Strategy
 break problem into similar
(smaller) subproblems
 recursively solve subproblems
 combine solutions to answer
5
Merge Sort cont.
[8, 3, 13, 6, 2, 14, 5, 9, 10, 1, 7, 12, 4]
[8, 3, 13, 6, 2, 14, 5] [9, 10, 1, 7, 12, 4]
[8, 3, 13, 6] [2, 14, 5]
[8, 3] [13, 6]
[8] [3][13] [6]
[2, 14] [5]
[2] [14]
[9, 10, 1] [7, 12, 4]
[9, 10] [1]
[9] [10]
[7, 12] [4]
[7] [12]
6
Merge Sort cont.
[3, 8] [6, 13]
[3, 6, 8, 13]
[8] [3][13] [6]
[2, 14]
[2, 5, 14]
[2, 3, 5, 6, 8, 13, 14]
[5]
[2] [14]
[9, 10]
[1, 9, 10]
[1]
[9] [10]
[7, 12]
[4, 7, 12]
[1, 4, 7, 9, 10,12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14]
[4]
[7] [12]
7
Merge Sort Procedure
 To sort the entire sequence
A ={A[1], A[2], . . . ,
A[ n]}, we make the initial
call MERGE-SORT( A, 1,
length[ A]), where length[
A] = n.
 The procedure MERGE-SORT(A, p, r) sorts the elements
in the sub-array A[ p…r].
 The divide step simply computes an index q that partitions
A[ p…r] into two sub-arrays: A[ p…q], containing n/2
elements, and A[ q + 1…r], containing n/2 elements.
8
9
Merge algorithm
10
Merge Sort
 The key operation of the merge sort algorithm is the
merging of two sorted sequences in the "combine" step.
To perform the merging, we use an auxiliary procedure
MERGE(A, p, q, r), where A is an array and p, q, and r
are indices numbering elements of the array such that p ≤
q < r.
 The procedure assumes that the sub-arrays A[ p…q] and
A[ q + 1…r] are in sorted order. It merges them to form a
single sorted sub-array that replaces the current sub-array
A[ p…r].
11
Merge algorithm cont.
 The operation of lines 10-17 in the call MERGE(A, 9, 12, 16).
12
Merge algorithm cont.
 The operation of lines 10-17 in the call MERGE(A, 9, 12, 16)
13
Analysis of Merge Sort
Statement Effort
 So T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1
MergeSort(A, left, right) { T(n)
if (left < right) { (1)
mid = floor((left + right) / 2); (1)
MergeSort(A, left, mid); T(n/2)
MergeSort(A, mid+1, right); T(n/2)
Merge(A, left, mid, right); (n)
}
}
14
Analysis of Merge Sort
 Divide: computing the middle takes O(1)
 Conquer: solving 2 sub-problem takes 2T(n/2)
 Combine: merging n-element takes O(n)
 Total:
T(n) = O(1) if n = 1
T(n) = 2T(n/2) + O(n) + O(1) if n > 1
 T(n) = O(n lg n)
 Solving this recurrence (how?) gives T(n) = O(n lg n)
 This expression is a recurrence
 To simplify the analysis we assume that the original
problem size is a power of 2. Each divide step then yields
two subsequences of size exactly n/2.
15
Analysis of Merge Sort cont.
Assume n=2k for k>=1
T(n) = 2 T(n/2) + bn + c
T(n/2) = 2T((n/2) /2) + b(n/2) + c
= 2[2T(n/4) + b(n/2) + c] + bn +c
= 4 T(n/4)+ bn +2c +bn +c
=4 T(n/4) + 2bn+ (1 + 2) c = 22 T(n/22)+2bn+(20+21)
= 4 [2T((n/4)/2) + b(n/4) + c] +2bn + (1+2)c
=8 T(n/8) + 3bn+ (1+2+4)c
=23 T(n/23) + 3bn+ (20+21+22)c
=2k T(n/2k) +kbn+(20+21+…+2k-1)c
T(1) = a, since n=2k  log n = log2k = k
T(n) = 2k. a + k bn + (20+21+…+2k-1) c , but
= b. n log n + (a + c) n – c
= O (n log n) Worst case
Ad

Recommended

Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
2.pptx
2.pptx
MohAlyasin1
 
10.1-introduction-to-complex-numbers.pdf
10.1-introduction-to-complex-numbers.pdf
onlyhumerasami
 
GRE - Math-for-students-taking-maths.pdf
GRE - Math-for-students-taking-maths.pdf
EdgarChavarria11
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
doc
doc
Siddharth Pujari
 
Solutions to the algebra of summation notation
Solutions to the algebra of summation notation
Mi L
 
Evaluating algebraic expression
Evaluating algebraic expression
Marites Ablay
 
Split and list technique for solving hard problems
Split and list technique for solving hard problems
Rohit Kumar Singh
 
Merge Sort
Merge Sort
Juan Zamora, MSc. MBA
 
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
Rai University
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
new math seminar paper
new math seminar paper
Charlesha Simmons
 
05 dc1
05 dc1
Hira Gul
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
dominicdaltoncaling2
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Divide and conquer
Divide and conquer
Vikas Sharma
 
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Vladimir Godovalov
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
Dimas Ruliandi
 
5617723.pptx
5617723.pptx
MatthewMhlongo
 
Unit 7 sorting
Unit 7 sorting
kalyanineve
 
B.tech ii unit-3 material multiple integration
B.tech ii unit-3 material multiple integration
Rai University
 
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
ruvirgandhi123
 
GraphTransformations.pptx
GraphTransformations.pptx
SrideviNagarjuna
 
Tema 8
Tema 8
adeodatopadilla
 
Merge sort
Merge sort
DrHimani Mittal
 
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
anasKhalaf4
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 

More Related Content

Similar to Algorithms and Data structures: Merge Sort (20)

Split and list technique for solving hard problems
Split and list technique for solving hard problems
Rohit Kumar Singh
 
Merge Sort
Merge Sort
Juan Zamora, MSc. MBA
 
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
Rai University
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
new math seminar paper
new math seminar paper
Charlesha Simmons
 
05 dc1
05 dc1
Hira Gul
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
dominicdaltoncaling2
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Divide and conquer
Divide and conquer
Vikas Sharma
 
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Vladimir Godovalov
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
Dimas Ruliandi
 
5617723.pptx
5617723.pptx
MatthewMhlongo
 
Unit 7 sorting
Unit 7 sorting
kalyanineve
 
B.tech ii unit-3 material multiple integration
B.tech ii unit-3 material multiple integration
Rai University
 
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
ruvirgandhi123
 
GraphTransformations.pptx
GraphTransformations.pptx
SrideviNagarjuna
 
Tema 8
Tema 8
adeodatopadilla
 
Merge sort
Merge sort
DrHimani Mittal
 
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
anasKhalaf4
 
Split and list technique for solving hard problems
Split and list technique for solving hard problems
Rohit Kumar Singh
 
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
BSC_COMPUTER _SCIENCE_UNIT-2_DISCRETE MATHEMATICS
Rai University
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
5-Solving-Quadratic-Equations-and-Rational-Algebraic-Equationsssssss.pptx
dominicdaltoncaling2
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Divide and conquer
Divide and conquer
Vikas Sharma
 
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Vladimir Godovalov
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
Dimas Ruliandi
 
B.tech ii unit-3 material multiple integration
B.tech ii unit-3 material multiple integration
Rai University
 
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
ruvirgandhi123
 
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
anasKhalaf4
 

Recently uploaded (20)

SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
Lesson-3_Program-Outcomes-and-Student-Learning-Outcomes_For-Students.pdf
Lesson-3_Program-Outcomes-and-Student-Learning-Outcomes_For-Students.pdf
SarahMaeDuallo
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
deep_learning_presentation related to llm
deep_learning_presentation related to llm
sayedabdussalam11
 
Communication_Skills_Class10_Visual.pptx
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
Shifting Focus on AI: How it Can Make a Positive Difference
Shifting Focus on AI: How it Can Make a Positive Difference
1508 A/S
 
Statistics-and-Computer-Tools-for-Analyzing-of-Assessment-Data.pptx
Statistics-and-Computer-Tools-for-Analyzing-of-Assessment-Data.pptx
pelaezmaryjoy90
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
reporting monthly for genset & Air Compressor.pptx
reporting monthly for genset & Air Compressor.pptx
dacripapanjaitan
 
MRI Pulse Sequence in radiology physics.pptx
MRI Pulse Sequence in radiology physics.pptx
BelaynehBishaw
 
Data Visualisation in data science for students
Data Visualisation in data science for students
confidenceascend
 
Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
Introduction for GenAI for Faculty for University.pdf
Introduction for GenAI for Faculty for University.pdf
Saeed999312
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
SUNSSE Engineering Introduction 2021.pdf
SUNSSE Engineering Introduction 2021.pdf
Ongkino
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
Lesson-3_Program-Outcomes-and-Student-Learning-Outcomes_For-Students.pdf
Lesson-3_Program-Outcomes-and-Student-Learning-Outcomes_For-Students.pdf
SarahMaeDuallo
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
deep_learning_presentation related to llm
deep_learning_presentation related to llm
sayedabdussalam11
 
Communication_Skills_Class10_Visual.pptx
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
Shifting Focus on AI: How it Can Make a Positive Difference
Shifting Focus on AI: How it Can Make a Positive Difference
1508 A/S
 
Statistics-and-Computer-Tools-for-Analyzing-of-Assessment-Data.pptx
Statistics-and-Computer-Tools-for-Analyzing-of-Assessment-Data.pptx
pelaezmaryjoy90
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
reporting monthly for genset & Air Compressor.pptx
reporting monthly for genset & Air Compressor.pptx
dacripapanjaitan
 
MRI Pulse Sequence in radiology physics.pptx
MRI Pulse Sequence in radiology physics.pptx
BelaynehBishaw
 
Data Visualisation in data science for students
Data Visualisation in data science for students
confidenceascend
 
Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
Introduction for GenAI for Faculty for University.pdf
Introduction for GenAI for Faculty for University.pdf
Saeed999312
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
SUNSSE Engineering Introduction 2021.pdf
SUNSSE Engineering Introduction 2021.pdf
Ongkino
 
Ad

Algorithms and Data structures: Merge Sort

  • 1. 1 Designing algorithms  There are many ways to design an algorithm.  Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1], we insert the single element A[ j] into its proper place, yielding the sorted sub-array A[1…j].  Another approach to design is the divide-and-conquer approach which has a recursive structure to solve a given problem;  they break the problem into several sub-problems that are similar to the original problem but smaller in size,  solve the sub-problems recursively,  and then combine these solutions to create a solution to the original problem.
  • 2. 2 The divide-and-conquer approach  Recursive in structure  Divide the problem into several smaller sub- problems that are similar to the original but smaller in size  Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner.  Combine the solutions to create a solution to the original problem
  • 3. 3 An Example: Merge Sort  Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each  Conquer: Sort the two subsequences recursively using merge sort.  Combine: Merge the two sorted subsequences to produce the sorted answer.
  • 4. 4 Merge Sort  To sort n numbers  if n = 1 done!  recursively sort 2 lists of numbers n/2 and n/2 elements  merge 2 sorted lists in O(n) time  Strategy  break problem into similar (smaller) subproblems  recursively solve subproblems  combine solutions to answer
  • 5. 5 Merge Sort cont. [8, 3, 13, 6, 2, 14, 5, 9, 10, 1, 7, 12, 4] [8, 3, 13, 6, 2, 14, 5] [9, 10, 1, 7, 12, 4] [8, 3, 13, 6] [2, 14, 5] [8, 3] [13, 6] [8] [3][13] [6] [2, 14] [5] [2] [14] [9, 10, 1] [7, 12, 4] [9, 10] [1] [9] [10] [7, 12] [4] [7] [12]
  • 6. 6 Merge Sort cont. [3, 8] [6, 13] [3, 6, 8, 13] [8] [3][13] [6] [2, 14] [2, 5, 14] [2, 3, 5, 6, 8, 13, 14] [5] [2] [14] [9, 10] [1, 9, 10] [1] [9] [10] [7, 12] [4, 7, 12] [1, 4, 7, 9, 10,12] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14] [4] [7] [12]
  • 7. 7 Merge Sort Procedure  To sort the entire sequence A ={A[1], A[2], . . . , A[ n]}, we make the initial call MERGE-SORT( A, 1, length[ A]), where length[ A] = n.  The procedure MERGE-SORT(A, p, r) sorts the elements in the sub-array A[ p…r].  The divide step simply computes an index q that partitions A[ p…r] into two sub-arrays: A[ p…q], containing n/2 elements, and A[ q + 1…r], containing n/2 elements.
  • 8. 8
  • 10. 10 Merge Sort  The key operation of the merge sort algorithm is the merging of two sorted sequences in the "combine" step. To perform the merging, we use an auxiliary procedure MERGE(A, p, q, r), where A is an array and p, q, and r are indices numbering elements of the array such that p ≤ q < r.  The procedure assumes that the sub-arrays A[ p…q] and A[ q + 1…r] are in sorted order. It merges them to form a single sorted sub-array that replaces the current sub-array A[ p…r].
  • 11. 11 Merge algorithm cont.  The operation of lines 10-17 in the call MERGE(A, 9, 12, 16).
  • 12. 12 Merge algorithm cont.  The operation of lines 10-17 in the call MERGE(A, 9, 12, 16)
  • 13. 13 Analysis of Merge Sort Statement Effort  So T(n) = (1) when n = 1, and 2T(n/2) + (n) when n > 1 MergeSort(A, left, right) { T(n) if (left < right) { (1) mid = floor((left + right) / 2); (1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); (n) } }
  • 14. 14 Analysis of Merge Sort  Divide: computing the middle takes O(1)  Conquer: solving 2 sub-problem takes 2T(n/2)  Combine: merging n-element takes O(n)  Total: T(n) = O(1) if n = 1 T(n) = 2T(n/2) + O(n) + O(1) if n > 1  T(n) = O(n lg n)  Solving this recurrence (how?) gives T(n) = O(n lg n)  This expression is a recurrence  To simplify the analysis we assume that the original problem size is a power of 2. Each divide step then yields two subsequences of size exactly n/2.
  • 15. 15 Analysis of Merge Sort cont. Assume n=2k for k>=1 T(n) = 2 T(n/2) + bn + c T(n/2) = 2T((n/2) /2) + b(n/2) + c = 2[2T(n/4) + b(n/2) + c] + bn +c = 4 T(n/4)+ bn +2c +bn +c =4 T(n/4) + 2bn+ (1 + 2) c = 22 T(n/22)+2bn+(20+21) = 4 [2T((n/4)/2) + b(n/4) + c] +2bn + (1+2)c =8 T(n/8) + 3bn+ (1+2+4)c =23 T(n/23) + 3bn+ (20+21+22)c =2k T(n/2k) +kbn+(20+21+…+2k-1)c T(1) = a, since n=2k  log n = log2k = k T(n) = 2k. a + k bn + (20+21+…+2k-1) c , but = b. n log n + (a + c) n – c = O (n log n) Worst case