11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

70 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>simplifying rule (4) it has cost c 3 i. The outer for loop is executed n times,but each time the cost of the inner loop is different because it costs c 3 i withi changing each time. You should see th<strong>at</strong> for the first execution of the outerloop, i is 1. For the second execution of the outer loop, i is 2. Each timethrough the outer loop, i becomes one gre<strong>at</strong>er, until the last time throughthe loop when i = n. Thus, the total cost of the loop is c 3 times the sum ofthe integers 1 through n. From Equ<strong>at</strong>ion 2.1, we know th<strong>at</strong>n∑ n(n + 1)i = ,2i=1which is Θ(n 2 ). By simplifying rule (3), Θ(c 1 + c 2 n + c 3 n 2 ) is simplyΘ(n 2 ).Example 3.12 Compare the asymptotic analysis for the following twocode fragments:sum1 = 0;for (i=1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!