0% found this document useful (0 votes)
29 views1 page

ECE 606, Fall 2019, Assignment 3: Zhijie Wang, Student ID Number: 20856733 Zhijie - Wang@uwaterloo - Ca September 24, 2019

Uploaded by

hstrybest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views1 page

ECE 606, Fall 2019, Assignment 3: Zhijie Wang, Student ID Number: 20856733 Zhijie - Wang@uwaterloo - Ca September 24, 2019

Uploaded by

hstrybest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ECE 606, Fall 2019, Assignment 3

Zhijie Wang, Student ID number: 20856733

[email protected]
September 24, 2019

1. We observed that the algorithm is recursive, then, when the input is < 1, n > we can write the running-
time of Add as following.
(
Θ(1) if n = 1
T (n) =
T (dn/2e) + T (bn/2c) + Θ(1) + Θ(1) otherwise

If n = 1, then, the algorithm will return at the Line 2. Thus, the running-time should be Θ(1). If n > 1,
then, for the Line 3, it takes Θ(1) to compute b p+q 2 c. For the Line 4, it takes T (dn/2e) to compute
Add(p, m) and T (bn/2c) to compute Add(m + 1, q), and Θ(1) to compute the sum of them.
Now, if n > 1, suppose n = 2k , we can rewrite the equation above as following:

n
T (n) = 2T ( ) + Θ(2)
2
n
= 2(2T ( 2 ) + 2) + 2
2
n
= 23 (T ( 3 )) + 23 + 2
2
= ...
= 2k−1 + 2k − 2
3
= ∗ 2k − 2
2
= Θ(2k )
= Θ(n)

Therefore, when the input is < 1, n >, the running-time will be Θ(n) or said Θ(2k ), where k = lg n.
2. Each A[i] has a size of m bits. Then, for each loop Line (2) and Line (7) both have a running-time of
Θ(m). Now, for the worst-case, Line (5) will execute j times, then have a running time of j ∗ Θ(m).
Pn
Therefore, for the whole function, the worst-case running-time should be i=1 2Θ(m) + iΘ(m).
Finally, we can give a characterization of the worst-case running-time of this function as Θ(m)
3. Here we denote Xk as A[i], which k represent Line (3) has executed k times. When the Line (3) is executed
first time, the probability that A[i] is the median m is P r{X1 = m} = n1 . Then, the probability that Line
(3) will execute again is P r{X1 6= m} = 1 − P r{X1 = m}. Hence, for the second time the probability
1
that A[i] is the median is P r{X2 = m} = P r{X2 = m|X1 6= m} ∗ P r{X1 6= m} = n−1 ∗ (1 − n1 ) = n1 .
1
Therefore, P r{Xk = m} = n.
Pn
E(X) = i=1 i ∗ n1 = 1+n
2
Line (3) is expected to run (n + 1)/2 times when the input is A[1, . . . , n].
4. a3p4.py

You might also like