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.2 Best, Worst, <strong>and</strong> Average Cases 63n log log n log n n n log n n 2 n 3 2 n16 2 4 2 4 2 · 2 4 = 2 5 2 8 2 12 2 16256 3 8 2 8 8 · 2 8 = 2 11 2 16 2 24 2 2561024 ≈ 3.3 10 2 10 10 · 2 10 ≈ 2 13 2 20 2 30 2 102464K 4 16 2 16 16 · 2 16 = 2 20 2 32 2 48 2 64K1M ≈ 4.3 20 2 20 20 · 2 20 ≈ 2 24 2 40 2 60 2 1M1G ≈ 4.9 30 2 30 30 · 2 30 ≈ 2 35 2 60 2 90 2 1GFigure 3.2 Costs for growth rates representative of most computer algorithms.We can get some further insight in<strong>to</strong> relative growth rates for various algorithmsfrom Figure 3.2. Most of the growth rates that appear in typical algorithms areshown, along with some representative input sizes. Once again, we see that thegrowth rate has a tremendous effect on the resources consumed by an algorithm.3.2 Best, Worst, <strong>and</strong> Average CasesConsider the problem of finding the fac<strong>to</strong>rial of n. For this problem, there is onlyone input of a given “size” (that is, there is only a single instance of size n foreach value of n). Now consider our largest-value sequential search algorithm ofExample 3.1, which always examines every array value. This algorithm works onmany inputs of a given size n. That is, there are many possible arrays of any givensize. However, no matter what array the algorithm looks at, its cost will always bethe same in that it always looks at every element in the array one time.For some algorithms, different inputs of a given size require different amountsof time. For example, consider the problem of searching an array containing nintegers <strong>to</strong> find the one with a particular value K (assume that K appears exactlyonce in the array). The sequential search algorithm begins at the first position inthe array <strong>and</strong> looks at each value in turn until K is found. Once K is found, thealgorithm s<strong>to</strong>ps. This is different from the largest-value sequential search algorithmof Example 3.1, which always examines every array value.There is a wide range of possible running times for the sequential search algorithm.The first integer in the array could have value K, <strong>and</strong> so only one integeris examined. In this case the running time is short. This is the best case for thisalgorithm, because it is not possible for sequential search <strong>to</strong> look at less than onevalue. Alternatively, if the last position in the array contains K, then the runningtime is relatively long, because the algorithm must examine n values. This is theworst case for this algorithm, because sequential search never looks at more than

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

Saved successfully!

Ooh no, something went wrong!