You are on page 1of 1

KIE1008 (Theory Tutorial) Session 2016/2017 (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
b. 5n3+2n+8
c. (n2+1)(3n+2)
d. 5(6n+4)
e. n + 2log(n)+6
f. 2nlog(n)+3n+6

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.


b. Repeat (a) if size = N.
c. What is the order of funcX()?

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.
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.
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.
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.
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