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

More Related Content

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

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

Recently uploaded (20)

PPTX
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
PDF
UNISE-Operation-Procedure-InDHIS2trainng
ahmedabduselam23
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PDF
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
 
PDF
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
PDF
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
PDF
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PPTX
Powerful Uses of Data Analytics You Should Know
subhashenia
 
PPTX
What Is Data Integration and Transformation?
subhashenia
 
PPTX
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
PDF
Group 5_RMB Final Project on circular economy
pgban24anmola
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PPTX
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
UNISE-Operation-Procedure-InDHIS2trainng
ahmedabduselam23
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
 
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
Powerful Uses of Data Analytics You Should Know
subhashenia
 
What Is Data Integration and Transformation?
subhashenia
 
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
Group 5_RMB Final Project on circular economy
pgban24anmola
 
BinarySearchTree in datastructures in detail
kichokuttu
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
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