Extra Credit Homework, 3% of your total grade or 3 extra points Due

advertisement
Extra Credit Homework, 3% of your total grade or 3 extra points
Due Oct 18th, 2011
1. (40 pts) Find the Big-O of the following code snippets:
a.
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
A[i] = random(n) // random takes constant time
sort(A, n) // sort takes n log n time
}
}
b. sum = 0
for (i = 0; i < n; i++){
for (j = 0; j < i; j++){
sum++;
}
}
c. k=1;
while(count < power(n,k)){ // power(n,k) = nk
if(k < 4){
k++;
}
print(” What a wonderful world!”);
}
d.
for(i=1; i<n; i*=i){
for(j = 0; j < m; j++){
print(”I really like algorithmic complexity”);
}
}
e.
if(i == 0){
for(j=10; j < n; j+=2){
k = random(j); // random takes constant time
}
}
else{
for(j=i; j < m; j+=5){
k = random(j); // random takes constant time
QuickSort(A, j); // sort takes j logj time
}
}
2. (20 pts) Give the Bog-Oh complexity in terms of n , of the following code:
c = 0;
for(i=0; i < n-1; i++){
s = 0;
for(j = 0; j < n-1; j++){
s = s+A[0];
for(k = 1; k < j; k++){
s = s+A[k];
}
}
if(B[i] == s){
c = c+1;
}
}
3. (20 pts) Order the following functions by asymptotic growth rate:
4𝑛 𝑙𝑜𝑔𝑛 + 2𝑛
210
𝑛𝑙𝑜𝑔𝑛
2
3𝑛 + 100 𝑙𝑜𝑔𝑛
4𝑛
2𝑛
2
𝑛 + 10𝑛
4.
(20 pts) Prove the following using the formal definition of Big-Oh complexity:
a. Prove that if an algorithm has: 𝑛2 + 10𝑛 + 3𝑛 simple operations, then its complexity is: 𝑂(10𝑛 )
b. Prove that if an algorithm has:100𝑛4 + 500𝑛 + 1000𝑙𝑜𝑔𝑛 simple operations, than its
complexity is: 𝑂(𝑛4 )
Download