You are on page 1of 4

KIE1008 (Theory Tutorial) Session 2018/2019 (Semester 2)

Tutorial 5: Big-O, Searching, Hashing and Sorting algorithms

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)

Sort the expressions above according to their order of complexity.

2. Consider the following function:


void funcX(int list[], int size) {
int sum = 0;
for (int index = 0; index < size; index++)
sum += list[index];

cout << “sum = “ << sum << endl;


}

a. Find the number of operations executed in funcX() if size = 10. 10


b. Repeat (a) if size = N. 10
c. What is the order of funcX()? O(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.

LINEAR PROBING INDICES QUADRATIC PROBING INDICES


15 15%101 = 15 15 15%101 = 15 15
101 101%101 = 0 0 101%101 = 0 0
116 116%101 15 16 116%101 15 16
210 210%101 = 8 8 210%101 = 8 8
0 0%101 = 0 1 0%101 = 0 1
98 98%101 =98 98 98%101 =98 98
21 21%101 = 21 21 21%101 = 21 21
217 217%101 = 15 17 217%101 = 15 19

6. Suppose that eight students with ID 197354883, 933185971, 132489992, 134152075,


216500325, 106500325, 216510325, 197354884. Suppose HT is of the side 19, indexed 0, 1, 2, …
18. Show how these students’ IDs are inserted in the HT in the order given, using hash function
h(k) = k%19. Use linear probing to resolve collision.

h(k) = k%19 INDEX


197354883 2 2
933185971 2 3
132489992 9 9
134152075 10 10
216500325 18 18
106500325 5 5
216510325 5 6
197354884 3 4
KIE1008 (Theory Tutorial) Session 2018/2019 (Semester 2)

7. For arr[12] = {11, 3, 2, 9, 6, 8, 12, 7, 10, 5, 1, 4}, show the steps of


descending-order sorting using all the sorting algorithms discussed (selection, bubble, insertion,
quick, merge, shell and heap). Compare the complexity of these sorting algorithms.

---END---

You might also like