Transcript Recursive Merge Sort Slides
Slide 1
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 2
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 3
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 4
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 5
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 6
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 7
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 8
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 9
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 10
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 11
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 2
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 3
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 4
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 5
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 6
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 7
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 8
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 9
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 10
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.
Slide 11
Divide and Conquer
Chapter 6
Divide and Conquer Paradigm
• Divide the problem into sub-problems of
smaller sizes
• Conquer by recursively doing the same
divisions on each part until small
manageable (solvable) size is reached.
• Combine the sub-solutions to form a
solution to the original problem.
• Read section 6.4 for more details.
Examples of divide and Conquer
Read the Recursive versions of
• Min-Max algorithm in Section 6.1
• Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg.
Input: A[1..n]
Output: A[1..n] sorted non-decreasingly
mergesort(A,1,n)
Procedure mergesort(A, low, high)
If low < high then
mid (low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
Merge(A, low, mid, high) % seen in Ch 1
End if
View
Top-down vs Bottom-up
• Bottom-up: iterative, merging is done
level by level from bottom to up.
• Top-down. Recursive, merging is done
in DFS (post-order) traversal.
Analysis
Theorem: Let n be a power of 2, and C(n)
be the number of comparisons in topdown MergeSort on A[1..n]. Then
n/2 log n C(n) nlog n –n +1
Proof: assume n = 2k
Best Case
C(1)=0, and
C(n) = 2 C(n/2) + n/2
= 22 C(n/22) + 2 x n/22 + n/2
= 22 C(n/22) + 2 x n /2
= 23 C(n/23) + 22 x n/23 + 2 x n/2
= 23 C(n/23) + 3 x n/2
= 2k C(n/2k) + k x n/2
= 2k C(1) + k x n/2 = n/2 log n
Worst Case
C(1)=0, and
C(n) = 2 C(n/2) + n-1
= 22 C(n/22) + n-21 + n-20
= 23 C(n/23) + n-22 + n-21 + n-20
= 2k C(n/2k) + n-2k-1 + ...+n-21 + n-20
= k n - 2k +1 = n log n – n + 1
Theorem
• Both types of MergeSort use (n log n)
• They have same performance if n is a
power of 2
• But different if n is not a power of 2.