Assignment 02
Assignment 02
a. Addition
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] = b[i][j] + c[i][j];
b. Multiplication
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
sum = 0;
for(k = 0; k < n; k++)
sum = sum + b[i][k] * c[k][j];
a[i][j] = sum;
}
}
c. Transposition
for(i = 0; i < n - 1; i++)
for(j = i+1; j < n; j++)
{
tmp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tmp;
}
Problem 3. Find the complexity of the following function:
a. 4n2 + 7n + 1 = O(n2)
b. n2 – 3n + 1 = Ω(n)
c. log(2n + k) = Q(log(n)), where k is a constant
n
d. å log(i) = O(n log(n))
i =1
Problem 5. Give an efficient algorithm to determine if there exists an integer i such that
ai = i in an array of integers a1 < a2 < a3 < . . . < an. What is the running time of your
algorithm?