12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 3.5 Calculating the Running Time for a Program 75but each time the cost of the inner loop is different because it costs c 3 i withi changing each time. You should see that 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 greater, until the last time throughthe loop when i = n. Thus, the <strong>to</strong>tal cost of the loop is c 3 times the sum ofthe integers 1 through n. From Equation 2.1, we know thatn∑i =i=1n(n + 1),2which 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 asymp<strong>to</strong>tic 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!