Algorithm Analysis Worksheet

advertisement
Algorithm Analysis
Worksheet
Program Analysis
1. Define assertion.
2. Define loop invariant?
3. What is the loop invariant for the following code segment?
public int summation(int n)
{
int sum = 0;
for(int i=0; i < n; i++)
{
sum += i:
}
return sum;
}
sum variable = _______________________________________
Loop control variable (i) = ______________________________
4. Define worst-case, best-case, and average case.
Big-O
5. What is the purpose of Big-O notation?
6. When classifying an algorithm using Big-O notation what is your principle
concern?
7. What is the Big-O for the following algorithm: _____________________
n(n – 1) 2
Common Algorithms
8. Place the following Big-O run-times in order from most efficient to least efficient.
O(log n), O(n3), O(1), O(n log n), O(n2)
____________________________________________________________
9. What is the Big-O for the following algorithms:
Linear Search –
Binary Search –
Selection Sort –
Insertion Sort –
Quick Sort –
Merge Sort –
Heap Sort –
Determining Big-O
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.
Example
How many stars would be displayed by the following algorithm? 1000
constant
n times
int n = 10;
for(int nA = 0; nA < n; nA++) {
for(int nB = 0; nB < n; nB++) {
for(int nC = 0; nC < n; nC++) {
n times
System.out.println(“*”);
}
}
constant
}
Big-O notation: O(n3)
n times
Justify your answer: Total = c + n * n * n * c = c + cn3
10. How many stars would be displayed by the following algorithm? ________
int nNum = 10;
for(int nOuter = 0; nOuter < nNum; nOuter++)
for(int nInner = 0; nInner < nNum; nInner++)
System.out.print("*");
Big-O notation: __________________ Justify your answer: ______________________
11. How many stars would be displayed by the following algorithm? ________
int nNum = 10;
for(int nOuter = 0; nOuter < nNum; nOuter++)
System.out.print("*");
Big-O notation: __________________ Justify your answer: ______________________
12. How many stars would be displayed by the following algorithm? ________
int nNum = 10;
for(int nOuter = 1; nOuter <= nNum; nOuter*=2)
System.out.print("*");
Big-O notation: __________________ Justify your answer: ______________________
13. How many stars would be displayed by the following algorithm? ________
int nNum = 10;
char[] caStars = new char[100];
for(int nI = 0; nI < 100; nI++)
caStars[nI] = '*';
System.out.print("" + caStars[nNum]);
Big-O notation: __________________ Justify your answer: ______________________
14. How many stars would be displayed by the following algorithm? ________
int nNum = 5;
int nStars = 1;
while(nNum > 0)
{
nStars*=2;
nNum--;
}
for(int nI = 0; nI < nStars; nI++)
System.out.print("*");
Big-O notation: __________________ Justify your answer: ______________________
15. How many stars would be displayed by the following algorithm? ________
int nNum = 10;
for(int nOuter = 1; nOuter <= nNum; nOuter++)
for(int nInner = 1; nInner <= nNum; nInner*=2)
System.out.print("*");
Big-O notation: __________________ Justify your answer: ______________________
16. How many stars would be displayed by the following algorithm? ________
public static void main(String[] args) {
int nNum = 10;
printStar(nNum);
}
public static void printStar(int nNum){
if(nNum > 0){
System.out.print('*');
printStar(nNum - 1);
}
}
Big-O notation: __________________ Justify your answer: ______________________
17. How many stars would be displayed by the following algorithm? ________
public static void main(String[] args) {
int nNum = 10;
printStar(nNum);
}
public static void printStar(int nNum){
if(nNum > 0){
System.out.print('*');
printStar(nNum/2);
}
}
Big-O notation: __________________ Justify your answer: ______________________
Download