2. [6] Below is the mystery algorithm that we worked on in Problem Set 4.
1
def mystery(lst):
2 if len(lst) <= 1:
3 return
4 if lst[0] > lst[-1]:
5 lst[0], lst[-1] = lst[-1], lst[0]
6 if len(lst) >= 3:
7 split = len(lst) // 3
8 mystery(lst[0..len(lst) - split - 1])
9 mystery(lst[split..len(lst) - 1])
10 mystery(lst[0..len(lst) - split - 1])
We analyzed that the recurrence of the worst-case runtime of this algorithm is the following.
$T(n) = \begin{cases} c, & \text{if } n \le 2\\ 3T(2n/3) + d, & \text{if } n \ge 3 \end{cases}$
Some students have already realized that this algorithm is in fact a sorting algorithm. So in this problem set we will
formalize our understanding of this interesting sorting algorithm.
(a) Find the asymptotic upper-bound on the worst-case runtime of mystery using the master theorem. State clearly
which case of the master theorem applies.
(b) State the proper precondition and postcondition for the mystery function. Note: "proper" precondition means
that it is necessary and sufficient for the algorithm to work correctly. In particular, don't add unnecessary
conditions.
(c) Prove that mystery is correct according to the precondition and postcondition that you specified in (b). Note:
Be careful when finding the possible program paths, and state clearly which lines of code are executed for each
program path (use the line numbers).