3-midterm1-algorithms-exams-final-midterms-and-quizzes
3-midterm1-algorithms-exams-final-midterms-and-quizzes
Qatar University
College of Engineering
Department of Computer Science and Engineering
CS323- Design and Analysis of Algorithms –2014
Name: ID:
Midterm#1- 7/4/2014
Duration: 60 Minutes
Question#1: (5 pts)
a) Prove that: ( n4 -n + n 1/2 logn ) is O (n4).
logn + n is θ(n).
n5 + n is Ω (n2).
5n is o(7n)
Question#2: (5 pts)
T(0) = 3 ; T(1)=5
2- Write a divide and conquer algorithm to calculate T(n) by using the recurrence relation (1).
3- Find, then solve the recurrence relation giving the number of recursive calls of the
algorithm developed in (2) in terms of n.
Question#3: (5 pts)
Let A be an array of size 5 indexed from 1 to 5. Using sequential search, find the expression of the
average number of key comparisons in A, where the probability to find the searched key (x) in the
array at position i is (1/5i ).
Hint: Give first then the expression of the probability to not find the key x in A..
Question#4: (5 pts)
a)
//Begin Here
For i=1 to n
For j=1 to n
For k=1 to n*n
Print ('loop');
For j=1 to n
For k=1 to n
Print (“loop”);
//End here
b)
i=n;
While (i>=1)
{ j=1;
while (j<=n) j=j*2;
i=i/2;
}
Where n is a power of 2.
c)
for (i=1; i<=n; i++)
for (j=1;j<=i; j++)
for (l=1;l<=27;l++);
print ("Hello");
d)
Question#5: ( 5 pts)
a) (4 pts) Assume that you have an array A of sorted integer numbers of size n. By using a divide
and conquer algorithm, write an algorithm to find the total number of items smaller or equal that
some value V.
b) (1 pts) Find a recurrence equation of the time complexity. Solve this equation.
Question#6: (5 pts)
8 5 3 6 9 2 1 4
Let n be the size an array to sort. Compare the time complexity of Mergesort, Quicksort in the best,
average and worst cases.
Excellent Exam