Anúncio
Próximos SlideShares
Lec 2 algorithms efficiency complexity
Carregando em ... 3
1 de 2
Anúncio

### Algorithm question- Please answer all questions- and show work or expl.docx

1. Algorithm question. Please answer all questions, and show work or explain when necessary! Consider the following algorithm. ALGORITHM Mystery(n)//Input: A nonnegative integer n Sir- 0 for i leftarrow1 to n do S leftarrow S + i * i return S What does the algorithm compute? What is its basic operation? How many times is the basic operation executed? What is the efficiency class of this algorithm? Solution a} What does the algorithm compute? ----The algorithm computes the sum of squares of n numbers. S <--- 0 The sum is iitialized to 0 for i <---1 to n This is a loop which will execute n times. n is a non negative integer. S<----S + i * iÂ Â This statement in the loop computes the square of numbers 1 to n ie 1 2 + 2 2+ ...............+ n 2 return S b}. The basic operations are sum comparison and assignment . c}.The basic operation sum will be executed n times.Assignment and comparisons will also be executed n times each. d} The efficiency class of this algorithm is cubic ( n 3 )as formula is n(n+1)(2n +1)/6
Anúncio