Advertisement

Upcoming SlideShare

Lec 2 algorithms efficiency complexity

Loading in ... 3

Feb. 3, 2023•0 likes## 0 likes

•6 views## views

Be the first to like this

Show More

Total views

0

On Slideshare

0

From embeds

0

Number of embeds

0

Download to read offline

Report

Education

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 .

wviolaFollow

Advertisement

Advertisement

Advertisement

Lec 2 algorithms efficiency complexityAnaya Zafar

Unit 2 algorithmDabbal Singh Mahara

3- Consider the following recursive algorithm for computing the sum of.docxgeney3

Brief introduction to Algorithm analysis Anantha Ramu

DAA - chapter 1.pdfASMAALWADEE2

01 intro to algorithm--updated 2015Hira Gul

- 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

Advertisement