Fit 1008 MST-Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Question 1 [12 marks]

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.

(1) Is shaker sort stable? Explain your answer.

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.

Yes, it is possible. For example, for small N . f (N ) = O(g(N )) implies f (N ) is


bounded by c ∗ g(N ) only above some arbitrary (but finite) N threshold. Below
this threshold, anything goes.

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)

• Provide a tail-recursive version of the sum_of_digits algorithm.


def sum_of_digits_tr ( x ):
return sum_of_digits_tr_aux (x , 0)

def s um_of_digits_tr_aux (x , result ):


if x < 10:
return x + result
else :
return sum_of_digits_tr_aux ( x //10 , result + x %10)

– 3 marks for correctly setting up first call


– 3 marks for a solution that is tail-recursive
– 3 marks for handling base case correctly
– 4 marks for correct handling of recursive case

• 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))

– 3 marks for identifying best = worst.


– 3 marks for identifying meaningful input is number of digits
– 4 marks for correct explanation, O(d) where d is number of digits also valid.

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]

def my_function (k , a_list ):


i = 0
n = len ( a_list )
count = 0
# HERE
while i < n :
if a_list [ i ] % k == 0:
count +=1
i +=1
return count

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

arg2(a_list) 0x10014F20 0x7FFFEFFC


0x7FFFF000

Registers
$fp 0x7FFFEFF0
$sp 0x7FFFEFE4

Page 4 of 7
• 7 marks for local variables as follows:

– 2 marks correct value of variable i


– 2 marks correct value of variable n
– 2 marks correct value of variable count
– 1 mark for correct names in all local variables

• 3 marks for correct values in registers f p and sp (1.5 each)

• 10 marks for correctly saved arguments as follows:

– 1 mark for list argument name


– 1 mark for k argument name
– 4 marks for list argument content on stack
– 4 marks for k argument content on stack

• 10 marks for correctly saved f p and sp as follows:

– ra value correct address 4 marks


– fp value correct address 4 marks
– ra correct name 1 mark
– fp correct name 1 mark

• 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

#calculate n%base and print it


lw $t1 , n
lw $t2 , base
div $t1 , $t2
mfhi $a0 #extract remainder to print
addi $v0 , $0 , 1
syscall
• 2 marks for useful comments

# n = n // base • 2 marks for correct global variables


lw $t1 , n
lw $t2 , base • 5 marks for correct sycalls
div $t1 , $t2
mflo $t0 #extract n//base • 7 marks for business logic inside the loop
sw $t0 , n #store in n
• 4 marks for faithfulness
j n_greater_than_0 #jump back to check while condition

exit:
addi $v0 ,$0, 10 #exit syscall
syscall

Page 6 of 7
• 10 marks for correct looping as follows:

– 5 marks for correct condition


– 1 mark for looping back to condition
– 1 mark for correctly ending loop
– 3 marks for correctly updating counter

Page 7 of 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy