CP40064E Algorithms and Data Type S2 FT 2020-2021 v1
CP40064E Algorithms and Data Type S2 FT 2020-2021 v1
Title Coursework
Assignment: Coursework
• For this coursework, you should create a logbook containing your answers to the questions below. You can find
a template for the logbook in the Assessments section of the module on Blackboard.
• Coursework should be all printed and submitted either word or PFD document. Photos or scanning to
hand written work is not accepted and will not be marked.
• Any screenshot of your code should include comment that state your student name and ID within the
code such as:
1
Section 1 – Algorithm Efficiency
Question 1.1
Assume that each of the expressions below gives the processing time T(n) spent by an algorithm for
solving a problem of input size n. Specify the dominant term having the steepest increase in n. What
is O() in each case? Give your answers by completing the table.
[6 marks]
Question 1.2
What is the time complexity of the following three algorithms? Express your answer in terms of Big-
O notation and justify your answer:
A)
[2 marks]
2
B)
[2 marks]
C)
[2 marks]
3
Section 2 – Recursion
Question 1.2
For the following recursive function, complete the recursion trace on the diagram below.
return
?
sum(5) return
?
sum(4)
return
sum(3) ?
return
sum(2) ?
return
sum(1) ?
return
sum(0) ?
[6 marks]
Question 2.2
Write a Python function that returns the nth Fibonacci number, where n is an integer passed as a
parameter to the function.
[7 marks]
4
Section 3 – Stacks
Question 3.1
For the following Stack operations, complete the table below to show the contents of the stack after
each function call, and value returned – write ‘none’ if no value is returned by the function.
[6 marks]
Question 3.2
Evaluate the following postfix expressions, giving your answer as a single number:
a) 5 4 * 9 +
b) 8 3 7 * + 4 –
c) 16 12 + 4 * 2 /
[6 marks]
Question 3.3
Convert the following infix expressions to postfix:
a) 9 + 2 * 20 – 4
b) 4 + 5 * 7 / 3
c) (20 – 8) * (42 – 16) / (21 + 6)
[6 marks]
5
Section 4 – Queues
Question 4.1
Complete the following table to show the return value and contents of the queue after each function
call in the sequence, assuming the queue is initially empty. You can assume the code for the queue is
as shown below.
[7 marks]
6
Question 4.2
What is the output of the following code, assuming the queue is initially empty? You can assume the
code for the queue is as shown in Question 4.1.
[5 marks]
7
Section 5 – Linked Lists
Question 5.1
Write a Python program that creates an unordered, singly-linked list consisting of 8 items. Each item
in the linked list should be a number. You can use the code given in the lecture slides for your Node
and UnorderedList classes.
Your node class should contain the following functions:
• a constructor
• get_data() – returns the data in the node
• get_next() – returns the next node in the list
• set_data() – sets the data in the node to the value given as a parameter
• set_next() – sets the node that this node links to, to the node given as a parameter
Add code to create an UnorderedList object, and call its add() function to add 8 items to the list. Call
the is_empty(), size() and print_list() functions to show that they work. In your logbook, show your
code and the output when you run the code.
The 8 numbers you will add them to the list should be your student ID.
Example output:
[8 marks]
Question 5.2
Add a function called search() to your UnorderedList class in Question 5.1. This function should take
a number as a parameter and return True if the number is contained in the linked list, or False if the
number is not in the list.
Add code to your program to allow the user to enter a number, and search for the number in your
linked list. Display a message to the user indicating if the number they entered was found in the list
or not. In your logbook, include your code, and also output that shows you have tested the search
function for a number in the list and a number not in the list.
Example output:
[7 marks]
8
Section 6 – Sorting
Question 6.1
Bubble sort: use your student ID as list of integers to be sorted by a bubble sort algorithm. Show the
contents of the list for each stage of each pass through the list.
The list should be your 8-digit student ID.
Example: If your student ID is 21234567 then your list will be
[2, 1, 2, 3, 4, 5, 6, 7]
[5 marks]
Question 6.2
Selection sort: use your student ID as list of integers to be sorted by a selection sort algorithm. Show
the contents of the list after each pass through the list.
The list should be your 8-digit student ID.
Example: If your student ID is 21234567 then your list will be
[2, 1, 2, 3, 4, 5, 6, 7]
[5 marks]
Question 6.3
Insertion sort: use your student ID as list of integers to be sorted by a insertion sort algorithm. Show
the contents of the list after each pass through the list.
The list should be your 8-digit student ID.
Example: If your student ID is 21234567 then your list will be
[2, 1, 2, 3, 4, 5, 6, 7]
[5 marks]
9
Section 7 – Searching
Question 7.1
Create a binary search tree by adding your student ID values in order:
Example: If your student ID is 21234567 then you will add the values in the order given
2, 1, 2, 3, 4, 5, 6, 7
[5 marks]
Question 7.2
The following diagram shows a binary tree with the root node containing the value 8. Write the pre-
order, in-order and post-order traversals of the binary tree shown in the diagram.
[10 marks]
10
Marking Criteria
1.1
1 mark for each correct answer
6 marks
1.2 1 mark for given correct Big O 2 marks for correct Big O notation
6 mark wrong answer
notation and justifying the answer
2.1 Complete diagram given with
6 marks Minimum given Partially given answer
correct values
2.2 Fully implemented function with
7 marks Incorrect function Partially implemented code
screenshots of the output
3:1 Some parts are Partially correct answer been Table been given with correct
6 marks relevant given Return Value and Stack content
3:2
6 marks 2 marks for each correct answer
3:3
6 marks 2 marks for each correct answer
4.1
7 marks Incorrect answer Partially correct answer Correct answer
4.2
5 marks Incorrect answer Partially correct answer Correct answer
11