Fit 1008 MST-Solution
Fit 1008 MST-Solution
Fit 1008 MST-Solution
In this part you are required to answer the following short questions. Your answer
should be concise. As a guideline, it should require no more space than the space that
is provided.
Yes it is stable, but care needs to be put into ensuring that comparisons are strict
inequalities.
Yes it is stable, because it is like bubble sort, comparing adjacent elements only,
thus it is stable.
3 marks for correct, 1 mark for unsatisfactory explanation.
(2) Is it possible for O(N ) code to run faster than O(1) code, explain your answer.
3 marks for this or some other well-argued reason. 1 mark if explanation not
completely satisfactory.
(3) How do best and worst case differ for merge-sort and why?
They don’t. Best and worst case merge-sort are O(N log(N )). Each recursive call
splits the list in half regardless of list content.
3 marks. 1 mark if explanation not completely satisfactory.
(4) What is the worst-case time complexity of quicksort and when does it happen?
O(N 2 ), when the pivot is happens to be reduce the size of the list by 1 in each
partition.
3 marks. 1 mark if explanation not completely satisfactory.
Page 2 of 7
Question 2 [23 = 13 + 10 marks]
The following function returns the sum of the digits composing a number x. For example,
sum_of_digits(35) will return 8.
def sum_of_digits ( x ):
if x < 10:
return x
else :
return ( x % 10) + sum_of_digits ( x //10)
• What are the best and worst-case time complexity of the non-tail recursive sum_of_digits.
Explain your answer – no explanation means no marks.
Best and worst case are the same. The operations in each recursion are O(1), thus
the complexity equals the recursive depth. The input x loses a digit each recur-
sion, so the time complexity is linear in the number of digits in x, which implies
O(log(x))
Page 3 of 7
2
Question 3 [35 marks]
Consider the following python code, which you wish to translate into MIPS.
a = 2
result = 0
l = [6 , 2 , 4]
result = my_function (a , l )
print ( result )
Complete the MIPS memory diagram below when code execution reaches the line
marked with the comment HERE. Include names and contents. Memory addresses are
provided. Before reaching my_function, ra = 0x00400000 and fp = 0x7FFF3114.
Name Content Address
l.length 3 0x10014F20
l[0] 6 0x10014F24
Heap
l[1] 2 0x10014F28
l[2] 4 0x10014F2C
0x10014F30
Stack 0x7FFFEFDC
0x7FFFEFE0
count 0 0x7FFFEFE4
n 3 0x7FFFEFE8
i 0 0x7FFFEFEC
saved $fp 0x7FFF3114 0x7FFFEFF0
saved $ra 0x00400000 0x7FFFEFF4
arg1(k) 2 0x7FFFEFF8
Registers
$fp 0x7FFFEFF0
$sp 0x7FFFEFE4
Page 4 of 7
• 7 marks for local variables as follows:
• 5 marks for heap content including list size (ignore globals in heap if they are put
in there)
Page 5 of 7
3
Question 4 [30 marks]
This question is about MIPS programming. Translate the following Python code faith-
fully into MIPS assembly language. Note that in this piece of code all variables are
global variables. Add comments to your MIPS code to facilitate readability. Use only
instructions or pseudo-instructions in the MIPS reference sheet.
n = 19
base = int ( input ())
while n > 0:
print ( n % base )
n = n // base
.data
#initialize global variables n and base
n: .word 19
base : .word 0
.text
# read integer input from user and store
addi $v0 , $0 , 5
syscall
sw $v0 , base # store input value in base
n_greater_than_0 :
#while n > 0:
lw $t0 , n
slt $t1, $0 , $t0 #is 0<n?
beq $t1,$0, exit #if not go to exit
exit:
addi $v0 ,$0, 10 #exit syscall
syscall
Page 6 of 7
• 10 marks for correct looping as follows:
Page 7 of 7