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.

60 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>seen so far. It is reasonable <strong>to</strong> assume that it takes a fixed amount of time <strong>to</strong>do one such comparison, regardless of the value of the two integers or theirpositions in the array.Because the most important fac<strong>to</strong>r affecting running time is normallysize of the input, for a given input size n we often express the time T <strong>to</strong> runthe algorithm as a function of n, written as T(n). We will always assumeT(n) is a non-negative value.Let us call c the amount of time required <strong>to</strong> compare two integers infunction largest. We do not care right now what the precise value of cmight be. Nor are we concerned with the time required <strong>to</strong> increment variablei because this must be done for each value in the array, or the timefor the actual assignment when a larger value is found, or the little bit ofextra time taken <strong>to</strong> initialize currlarge. We just want a reasonable approximationfor the time taken <strong>to</strong> execute the algorithm. The <strong>to</strong>tal time<strong>to</strong> run largest is therefore approximately cn, Because we must make ncomparisons, with each comparison costing c time. We say that functionlargest (<strong>and</strong> the largest-value sequential search algorithm in general)has a running time expressed by the equationT(n) = cn.This equation describes the growth rate for the running time of the largestvaluesequential search algorithm.Example 3.2 The running time of a statement that assigns the first valueof an integer array <strong>to</strong> a variable is simply the time required <strong>to</strong> copy the valueof the first array value. We can assume this assignment takes a constantamount of time regardless of the value. Let us call c 1 the amount of timenecessary <strong>to</strong> copy an integer. No matter how large the array on a typicalcomputer (given reasonable conditions for memory <strong>and</strong> array size), the time<strong>to</strong> copy the value from the first position of the array is always c 1 . Thus, theequation for this algorithm is simplyT(n) = c 1 ,indicating that the size of the input n has no effect on the running time.This is called a constant running time.

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

Saved successfully!

Ooh no, something went wrong!