midsol16

# Outside the while loop but inside the for loop an

• 9
• 100% (2) 2 out of 2 people found this document helpful

This preview shows pages 5–9. Sign up to view the full content.

Outside the while loop, but inside the for loop an additional constant number c 2 of operations are performed, so each iteration of the for loop performs c 2 + c 1 ( m - 1) operations. Since the for loop is repeated n - m times, the total number of operations is ( n - m )( c 2 + c 1 ( m - 1)). Ignoring constant terms, the time complexity is O ( nm ). 6

This preview has intentionally blurred sections. Sign up to view the full version.

13. [7 marks] Consider the following algorithm. A is an array of size n . Algorithm rec ( n ) In: Integer value n 1. if n = 1 then return A [0] else { i rec ( n - 1) A [ n - 1] A [ n - 1] + i return A [ n - 1] } Write a recurrence equation for the time complexity of this algorithm. f (1) = c , where c is the constant number of operations performed in the base case f ( n ) = c 1 + f ( n - 1) for n> 1, where c 1 is the constant number of operations performed by the algorithm ignoring the recursive call. 14. [6.5 marks] Solve the above recurrence equation by repeated substitution. You need to show how you solved the equation. [0.5 marks] Compute the order of the time complexity. f ( n ) = c 1 + f ( n - 1) f ( n - 1) = c 1 + f ( n - 2) f ( n - 2) = c 1 + f ( n - 3) . . . f (2) = c 1 + f (1) Replacing the value of f ( n - 1) in the expression for f ( n ) and then replacing the value of f ( n - 2) in the resulting expression, and so on, we get f ( n ) = c 1 + c 1 + c 1 + ··· + c 1 + f (1) = c 1 + c 1 + c 1 + ··· + c 1 + c. To determine the number of times that the term c 1 appears in the above summation, note that the above set of equations, for f (2) to f ( n ), includes n - 1 equations. Therefore, the term c 1 appears n - 1 times, and so f ( n ) = ( n - 1) c 1 + c . Ignoring constant terms, we get that f ( n ) is O ( n ). 7
15. Consider a hash table of size 7 with hash function h ( k ) = k mod 7. Draw the contents of the table after inserting, in the given order, the following values into the table: 9, 16, 27, 62, and 2: [4 marks] (a) when linear probing is used to resolve collisions [8 marks] (b) when double hashing with secondary hash function h ( k ) = 5 - ( k mod 5) is used to resolve collisions. 0 1 2 3 4 5 6 Linear probing 62 9 16 2 27 0 1 2 3 4 5 6 Double hashing 62 9 2 27 16 16. [2 marks] Consider the following binary search tree. Insert the key 23 and then insert the key 16 into the tree. Draw the tree after each insertion. You must use the algorithms described in class for inserting data in a binary search tree. 20 5 30 27 15 20 20 5 30 27 15 30 27 5 15 23 insert 23 Insert 16 23 16 8

This preview has intentionally blurred sections. Sign up to view the full version.

17. [4 marks] Consider the following binary search tree. Remove the key 15 from the tree and draw the resulting tree. Then remove the key 35 from this new tree and show the final tree (so in the final tree both keys, 15 and 35, have been removed). You must use the algorithms described in class for removing data from a binary search tree. 35 25 45 50 15 5 10 27 20 35 25 45 50 20 5 10 27 45 25 50 20 5 10 27 remove 15 remove 35 9
Western University Department of Computer Science CS2210A Midterm Examination November 7, 2016 9 pages, 17 questions 120 minutes Last Name: Fist Name: CS2210 Class list number: Student Number: PART I PART II 9 10 11 12 13 14 15 16 17 Total Instructions Write your name, CS2210 class list number, and student number on the space provided. Please check that your exam is complete. It should have 9 pages and 17 questions. The examination has a total of 100 marks. When you are done, call one of the TA’s and they will pick your exam up. 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern