
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The master theorem and its application to the analysis of heapify() and heapsort algorithms. The basics of the master theorem, its use in analyzing recursive functions, and the advantages of merge sort and insertion sort. Additionally, it discusses the implementation of heaps as complete binary trees and arrays, basic heap operations, and heapify() example. Lastly, it analyzes the running time of heapify() and heapsort algorithms.
Typology: Lecture notes
1 / 56
This page cannot be seen from the preview
Don't miss anything!

















































Review: Merge Sort
MergeSort(A, left, right) {
if (left < right) {
mid = floor((left + right) / 2);
MergeSort(A, left, mid);
MergeSort(A, mid+1, right);
Merge(A, left, mid, right);
}
}
// Merge() takes two sorted subarrays of A and
// merges them into a single sorted subarray of A.
// Code for this is in the book. It requires O(n)
// time, and does require allocating O(n) space
Review: Solving Recurrences
●
●
●
Review: Solving Recurrences
●
■ A.k.a. the “making a good guess method”
■
Guess the form of the answer, then use induction to
find the constants and show that solution works
■ Run an example : merge sort
○ T(n) = 2T(n/2) + cn
○ We guess that the answer is O(n lg n)
○ Prove it by induction
■ Can similarly show T(n) = Ω(n lg n), thus Θ(n lg n)
● T(n) =
aT(n/b) + cn
a(aT(n/b/b) + cn/b) + cn
a
2
T(n/b
2
) + cna/b + cn
a
2
T(n/b
2
) + cn(a/b + 1)
a
2
(aT(n/b
2
/b) + cn/b
2
) + cn(a/b + 1)
a
3
T(n/b
3
) + cn(a
2
/b
2
) + cn(a/b + 1)
a
3
T(n/b
3
) + cn(a
2
/b
2
…
a
k
T(n/b
k
) + cn(a
k-
/b
k-
k-
/b
k-
2
/b
2
cn n
b
n
aT
T n
●
So we have
■ T(n) = a
k
T(n/b
k
) + cn(a
k-
/b
k-
2
/b
2
● For k = log
b
n
■ n = b
k
■ T(n) = a
k
T(1) + cn(a
k-
/b
k-
2
/b
2
= a
k
c + cn(a
k-
/b
k-
2
/b
2
= ca
k
k-
/b
k-
2
/b
2
= cna
k
/b
k
k-
/b
k-
2
/b
2
= cn(a
k
/b
k
2
/b
2
cn n
b
n
aT
T n
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
cn n
b
n
aT
T n
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
■ Recall that (x
k
k-
k+
-1)/(x-1)
cn n
b
n
aT
T n
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
■ Recall that (x
k
k-
k+
-1)/(x-1)
■ So:
■ T(n) = cn ·(1) = (n)
cn n
b
n
aT
T n
a b
a b
a b
b
a
b
a
b
a
k k
k
k
k
k
1
1
1
1
1
1
1
1 1
1
1
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
cn n
b
n
aT
T n
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
■ T(n) = cn · (a
k
/ b
k
)
cn n
b
n
aT
T n
k
k
k
k
k
k
a b
a b
a b
b
a
b
a
b
a
1
1
1
1
1
1
●
b
■ T(n) = cn(a
k
/b
k
2
/b
2
●
■ T(n) = cn · (a
k
/ b
k
)
= cn · (a
log n
/ b
log n
) = cn · (a
log n
/ n)
cn n
b
n
aT
T n
k
k
k
k
k
k
a b
a b
a b
b
a
b
a
b
a
1
1
1
1
1
1
● So with k = log
b
n
■ T(n) = cn(a
k
/b
k
2
/b
2
●
What if a > b?
■ T(n) = cn · (a
k
/ b
k
)
= cn · (a
log n
/ b
log n
) = cn · (a
log n
/ n)
recall logarithm fact: a
log n
= n
log a
= cn · (n
log a
/ n) = (cn · n
log a
/ n)
cn n
b
n
aT
T n
k
k
k
k
k
k
a b
a b
a b
b
a
b
a
b
a
1
1
1
1
1
1
● So with k = log
b
n
■ T(n) = cn(a
k
/b
k
2
/b
2
●
What if a > b?
■ T(n) = cn · (a
k
/ b
k
)
= cn · (a
log n
/ b
log n
) = cn · (a
log n
/ n)
recall logarithm fact: a
log n
= n
log a
= cn · (n
log a
/ n) = (cn · n
log a
/ n)
= (n
log a
)
cn n
b
n
aT
T n
k
k
k
k
k
k
a b
a b
a b
b
a
b
a
b
a
1
1
1
1
1
1