# This tutorial gives you more exercises on big Oh-nations. ```CS2468 Tutorial (Week 3)
This tutorial gives you more exercises on big Oh-nations.
Give a big-Oh characterization for question 1 and question 2.
1. Algorithm MatrixMultiplication(matrix A, matrix B)
Input
two matrices: matrix A (m*n) and matrix B (n*p)
Output matrix C (m*p) = AB
for i  0 to m-1 do
for j  0 to p-1 do
Ci, j  0
……………………m
……………………pm
……………………pm
for k  0 to n-1 do
……………………npm
Ci, j  Ci, j + Ai, k * Bk, j ……………………npm
return C
……………………1
Solution：The time complexity is m+2pm+2npm+1=O(npm).
2. Algorithm binarySearch (int sortedArray[ ], int n, int key)
Input
a sorted array sortedArray in increasing order (supposed that no
two elements are equal to each other), the length of the array n, and an
element to be searched for key
Output position of the element key or -1 if the element is not in the array
low  0
high  n – 1
while low ≤ high
mid  (low + high) / 2
if sortedArray[mid] = key
return mid
if sortedArray[mid] &lt; key
low  mid + 1
else
high  mid – 1
return -1
……………………1
……………………1
……………………k
……………………k
……………………k
……………………1
……………………k
……………………k
……………………k
……………………k
……………………1
Solution: Suppose the while loop runs k times. Every time the while loop is run,
the search space is reduced to the half of the number of the remaining elements.
The while loop terminates till the number of elements in the search space is 1. So
we can get that
n
2
k 1
 1 and
n
2k
 1
so k  log 2 n .
Therefore, the time complexity is 7k+4=O(k)=O(logn).
3. Given the Node class as
public class Node {
// Instance variables:
private Object element;
private Node next;
/** Creates a node with null references to its element and next node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(Object e, Node n) {
element = e;
next = n;
}
// Accessor methods:
public Object getElement() {
return element;
}
public Node getNext() {
return next;
}
// Modifier methods:
public void setElement(Object newElem) {
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}
How can you use it to implement the Stack and Queue interfaces? What is the
time cost for each stack and queue operations?
Extra Exercise:
1)
2 * f(n/2)
if n≥2
1
if n=1
f(n) =
What is the value of f(n) in terms of n?
2)
3 * f(n/3)
if n≥2
1
if n=1
f(n) =
What is the value of f(n) in terms of n?
3)
f(n/3)
if n≥2
f(n) =
1
if n=1
What is the value of f(n) in terms of n?
```