Tutorial9 (With Ans)
Tutorial9 (With Ans)
1. What is the order of these expressions, which represent the number of operations for certain
algorithms:
a. n2+6n+4 O(n^2)
b. 5n3+2n+8 O(n^3)
2
c. (n +1)(3n+2) O(n^2)
d. 5(6n+4) O(n)
e. n + 2log(n)+6 O(log n)
f. 2nlog(n)+3n+6 O(n log n)
3. For a function that uses a loop to find the sum of the squares of all integers between 1 and n.
what is the order of the function? What is the function is calculating sum of the factorial of n.
O(n)
Recursive funtion
KIE1008 (Theory Tutorial) Session 2018/2019 (Semester 2)
4. For arr[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, show the steps of searching each
number using binary search. Compare the average number of comparison needed for binary
search and linear search.
Find key = 9
Found = false
First = 0
Last = 9
Mid = 4
arr[4]<9
first = mid +1 (first = 5)
mid = (first +last)/2 (mid = 7)
arr[7] = key
found = true
LINEAR BINARY
2 1 3
3 2 2
4 3 3
5 4 4
6 5 1
7 6 4
8 7 3
9 8 2
10 9 3
11 10 4
TOTAL 55 29
AVERAGE 5.5 2.9
KIE1008 (Theory Tutorial) Session 2018/2019 (Semester 2)
5. Suppose that the size of a hash table is 101, and that certain keys with the indices 15, 101, 116,
210, 0, 98, 21 and 217 are to be inserted in this order into an initially empty hash table. Using
modular arithmetic, find the indices in the hash table if linear probing is used. Repeat if
quadratic probing is used instead.
---END---