Algorithmic Analysis Test

advertisement
Algorithmic Analysis
Test
1. A precise statement, in terms of the loop variables, of what is true before and after each
iteration of the loop is called a
a. precondition
b. postcondition
c. loop invariant
d. variable initialization
e. increment analysis
2. What is the purpose of Big-O notation?
Provides a method of classifying algorithms by their run-time efficiency so that you
do not have test the algorithms to determine their speed.
3. What is the time complexity for deleting the first element of an array?
a. O(1)
b. O(log n)
c. O(n)
d. O(n log n)
e. O(n2)
4. What is the time complexity for deleting the last element of an array?
a. O(1)
b. O(log n)
c. O(n)
d. O(n log n)
e. O(n2)
5. Assume that an array stores numbers in a random sequence. What is the time complexity
for searching for a specified number with the most efficient search algorithm possible?
a. O(1)
b. O(log n)
c. O(n)
d. O(n log n)
e. O(n2)
6. Assume that an array stores numbers in a sorted sequence. What is the time complexity
for searching for a specified number with the most efficient search algorithm possible?
a. O(1)
b. O(log n)
c. O(n)
d. O(n log n)
e. O(n2)
Instructions: The following algorithms will display a different number of stars, depending on the
value in nNum. Classify the Big-O growth rate, and justify your answer with a calculation.
7. How many stars would be displayed by the following algorithm? ___100_____
int nNum = 10;
for(int nOuter = 0; nOuter < nNum; nOuter++)
for(int nInner = 0; nInner < nNum; nInner++)
System.out.print("*");
Big-O notation: ____O(n2)______________ Justify your answer: ____c+ c * n * n__= c + cn2___
8. How many stars would be displayed by the following algorithm? ___10_____
int nNum = 10;
for(int nOuter = 0; nOuter < nNum; nOuter++)
System.out.print("*");
Big-O notation: _______O(n)_____ ____ Justify your answer: ______c + c * n = c + cn______
9. How many stars would be displayed by the following algorithm? ___1000_____
int nNum = 10;
for(int nA = 0; nA < nNum; nA++)
for(int nB = 0; nB < nNum; nB++)
for(int nC = 0; nC < nNum; nC++)
System.out.print("*");
Big-O notation: _____O(n3)____________ Justify your answer: ____c+c*n*n*n = c + cn3____
10. How many stars would be displayed by the following algorithm? ___6_____
public static void main(String[] args)
{
int nNum = 3;
int nLimit = mystery(nNum);
for(int nA = 0; nA < nLimit; nA++)
System.out.print("*");
}
public static int mystery(int nNum)
{
if(nNum == 0)
return 1;
else
return nNum * mystery(nNum-1);
}
Big-O notation: ______O(n)____________ Justify your answer: __c+n+cn+2c = 3c+(c+1)n__
Download