## ICE-2201-0: Data Structures and Algorithms

### Past Exam

Answer ALL questions (Total marks 100)1. (a) [4]Time complexity of an algorithm is typically represented using big O notation, whichis concerned with counting characteristic operations (e.g., arithmetic operations inmath algorithms, comparisons in searching algorithms). Two alternatives to the useof big O notation would be measuring time in seconds and counting algorithms steps.In both cases, describe the advantages and disadvantages of using that approach.(b) [3]The following shows the elapsed time (in seconds) of two sorting algorithms overdifferent sizes of data to be sorted:Number of items to be sorted 10 20 30 40Algorithm A 3.5 4.8 5.9 6.2Algorithm B 4.0 4.01 4.011 4.0111Among the two algorithms (Algorithm A and B), which one is more time-efficient?Justify your answer.(c) [3]State the time complexity of the following fragment of code/algorithm (Algorithm 1),using big 𝑂 notation:Algorithm 1for i = 0, 1,..., N-1 doa[i] = i*iend forfor j = 0, 1,..., N-1 dob[j] = j*j*jend forfor k = 0, 1,..., N-1 doc[k] = k*k*k*kend forJustify your answer.2 (d) Consider a Binary Search algorithm (Algorithm 2):Algorithm 2 To find which (if any) component of the sorted(sub)array a[left...right] equals target.Set 𝑙 = left, and set 𝑟 = right.while 𝑙 ≤ 𝑟 doLet m be (a) .if target equals 𝑎[𝑚] thenterminate with answer 𝑚.end ifif target is less than 𝑎[𝑚] thenset (b) .end ifif target is greater than 𝑎[𝑚] thenset (c) .end ifend whileTerminate with answer none.i. [3]Complete the missing parts of the algorithm at (a), (b) and (c).ii. [3]Using Big-O notation, state the time complexity of the algorithm; justify youranswer.iii. [3]At each iteration of the algorithm, it compares the target with the current sub-arrayelement 𝑎[𝑚]. For the target value = ‘5’ and the following sorted array:‘1’, ‘2’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘10’, ‘15’,write values of 𝑎[𝑚] in sequence, which will be compared with the target value bythe algorithm.(e) i. [4]Write the insertion sort algorithm that sorts an array a[left. . .right] into ascendingorder.ii. [4]Given the following array,15 3 12 5 9run the insertion sort algorithm and show the intermediate status of the arrayuntil the sorting is completed.3 / troi drosodd/ turn over 2. (a) A bounded queue can be implement using an array.i. [2]The following diagram shows an example queue implemented using an ordinaryarray (i.e. front ≤ rear): .front =0 1 2 rear =3 4 5Lisa Bart MargeRedraw the diagram showing the queue status after inserting ’Maggie’ and adeletion.ii. [3]Explain a drawback of the queue implementation based on an ordinary array,using big-O notation.iii. [3]One can avoid the problem of the ordinary array-based implementation if a dif-ferent kind of an array is used. Explain this, using big-O notation.(b) [3]Consider an unknown data structure and the following operations, where a letterrepresents an insertion of that letter into the data structure and the asterisk * meansextracting and printing one number:P G U * Y A * * * B H O * X R * * *The data structure is initially empty. The output resulting from these operations is:U A Y G O R X HWhat is the data structure? Choose one structure amongst the following: a map, aqueue, a stack, and a set. Justify your answer.(c) [6]If a stack is to be implemented using a linked list, will you use a singly-linked list or adoubly-linked list? Justify your answer, using big-O notation. To Download Slides of python lab click on the link Python 1 Binary search treesThis lab is not assessed and is intended for revision and to test your understanding of binary search trees.Getting startedThe file bst_framework.py contains class and method stubs for creating a binary search tree. Your task is to complete the method stubs and check that your actual output matches the expected output in the __main__ function. You may program your solution in another programming language if you wish. At a minimum, you should be able to implement the search and insert methods. Information on how to implement all the methods can be found on Blackboard in the BST lecture slides and is provided below for convenience. You are also permitted to create additional helper functions where necessary.A tool for visualizing BSTs which can be useful for debugging can be found here:https://yongdanielliang.github.io/animation/web/BST.htmlSearch1: Set curr to the BST’s root2: repeat2.1: if curr is null then terminate with answer none2.2: else if target is equal to curr’s element then terminate with answer curr2.3: else if target is less than curr’s element then set curr to curr’s left child2.4: If if target is greater than curr’s element then set curr to curr’s right childInsert 1. Set parent to null, and set curr to the BST’s root.2. repeat:2.1. if curr is null, replace the null link from which curr was taken by a link to a newly created leaf node with element elem, and terminate.2.2. else if elem is equal to curr’s element, terminate.2.3. else if elem is less than curr’s element, set parent to curr and set curr to curr’s left child.2.4. else if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. Python ExerciseSet_1**Content to WhatsApp number for solutions **