exercise

4.1 Applying the master theorem

a) According to the third case of the master theorem

b) According to the first case of the master theorem

c) According to the second case of the master theorem

4.2 Asymptotic notations

a) b)

4.3 One-Looped Sort

a)

Iterationi after iterationA
11[10, 20, 30, 40, 50, 25]
22[10, 20, 30, 40, 50, 25]
33[10, 20, 30, 40, 50, 25]
44[10, 20, 30, 40, 50, 25]
55[10, 20, 30, 40, 50, 25]
64[10, 20, 30, 40, 25, 50]
73[10, 20, 30, 25, 40, 50]
82[10, 20, 25, 30, 40, 50]
93[10, 20, 25, 30, 40, 50]
104[10, 20, 25, 30, 40, 50]
115[10, 20, 25, 30, 40, 50]
126[10, 20, 25, 30, 40, 50]
b)
Invariant: At the moment when the variable gets incremented to a new value for the first time, the first k elements of the array are sorted in increasing order.
Base: , The first one element (or ) is always sorted
IS:
  • If , then the first elements are already sorted. Then is incremented to , so that the next new position will be inspected.
  • If , then the algorithm swaps and and is set to . This step repeatedly moves the current unsorted element to the left, until it gets to the right position. After the element gets to the right position, the first elements are sorted. will then be continuously incremented until it meets a new value , so that the next new position will be inspected. By induction, every time is increased to k, is sorted.

c)

4.4 Searching for the summit

a)


while do: if then return else if \qquad$$l\leftarrow m+1 else \qquad$$r\leftarrow m-1 return -1


The code uses binary search to find the summit. In each iteration the search space for the summit is halved. Thus, we must have the worst-case running time

b) Again, we can first find the summit in time. Then we use binary search to try to find in the sorted array , which takes time. If we don’t find in the first part of the array, we then perform binary search on the second part , which is also sorted and also takes time. Since each step requires at most time, the worst-case running time of our algorithm is .

4.5 Counting function calls in loops

a)

b) According to the Master Theorem,