Professional Documents
Culture Documents
Written Assignment 1
1. 0.9999100n < 5log(n+100)10 < nlg20n < 1000n2+10lgn+1 < 1.00010.01n < 2n < (0.001n)n < n!
(8 marks)
Hint: according to Stirling's approximation, i.e. ! 2 , we have:
!
lim = lim 2 = lim 2 (370.37) =
(0.001) 0.001
Hence, ! grows faster than (0.001)
2.
(a) (5 marks)
Let T(n) be the running time of the method call func(n). There are log n iterations for the while
loop, and within the loop body the running time is some constant value, thus:
() = log() = (log )
(b) (5 marks)
Let T(n) be the running time of the method call func(n). There are lg n iterations for the outter
while loop, n iterations for the inner loop, and within the inner loop body the running time is
some constant value, thus:
() = = ( log )
3.
(a) (5 marks)
Input Stack Output Input Stack Output
A empty A C +*( ABC-C
+ + A + +*(+ ABC-C
( +( A D +*(+ ABC-CD
B +( AB ) +* ABC-CD+
- +(- AB / +/ ABC-CD+*
C +(- ABC E +/ ABC-CD+*E
) + ABC- null empty ABC-CD+*E/+
* +* ABC- / +/ ABC-CD+*
( +*( ABC-
(b) (5 marks)
Detailed steps are as follows:
(c) (5 marks)
Preorder traversals: +A/*-BC+CDE
Inorder traversals: A+B-C*C+D/E
Postorder traversals: ABC-CD+*E/+
4.
(a) Detailed steps are as follows: (10 marks)
(b) Detailed steps are as follows: (10 marks)
5.
(a) The result is as follows: (5 marks)
6.
(a) The output is: 13, 6, 24, 3, 18 (2 marks)
Note: for question 6(c), if the length of 2D array is 10 and results are correct (i.e. deletion is done
in-place), full marks would also be given. If (i) and (ii) are done in separate 2D arrays and results
are correct, 2 marks would be deducted.
< END >