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...

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

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

Sec. 3.5 Calculating the Running Time for a Program 77then multiplied by the input value, which takes constant time. Thus, the cost ofthe fac<strong>to</strong>rial function, if we wish <strong>to</strong> measure cost in terms of the number of multiplicationoperations, is one more than the number of multiplications made by therecursive call on the smaller input. Because the base case does no multiplications,its cost is zero. Thus, the running time for this function can be expressed asT(n) = T(n − 1) + 1 for n > 1; T (1) = 0.We know from Examples 2.8 <strong>and</strong> 2.13 that the closed-form solution for this recurrencerelation is Θ(n).The final example of algorithm analysis for this section will compare two algorithmsfor performing search in an array. Earlier, we determined that the runningtime for sequential search on an array where the search value K is equally likely<strong>to</strong> appear in any location is Θ(n) in both the average <strong>and</strong> worst cases. We wouldlike <strong>to</strong> compare this running time <strong>to</strong> that required <strong>to</strong> perform a binary search onan array whose values are s<strong>to</strong>red in order from lowest <strong>to</strong> highest.Binary search begins by examining the value in the middle position of the array;call this position mid <strong>and</strong> the corresponding value k mid . If k mid = K, thenprocessing can s<strong>to</strong>p immediately. This is unlikely <strong>to</strong> be the case, however. Fortunately,knowing the middle value provides useful information that can help guidethe search process. In particular, if k mid > K, then you know that the value Kcannot appear in the array at any position greater than mid. Thus, you can eliminatefuture search in the upper half of the array. Conversely, if k mid < K, then youknow that you can ignore all positions in the array less than mid. Either way, halfof the positions are eliminated from further consideration. Binary search next looksat the middle position in that part of the array where value K may exist. The valueat this position again allows us <strong>to</strong> eliminate half of the remaining positions fromconsideration. This process repeats until either the desired value is found, or thereare no positions remaining in the array that might contain the value K. Figure 3.4illustrates the binary search method. Here is an implementation for binary search:// Return the position of an element in sorted array "A"// with value "K". If "K" is not in "A", return A.length.static int binary(int[] A, int K) {int l = -1;int r = A.length; // l <strong>and</strong> r are beyond array boundswhile (l+1 != r) { // S<strong>to</strong>p when l <strong>and</strong> r meetint i = (l+r)/2; // Check middle of remaining subarray}if (K < A[i]) r = i; // In left halfif (K == A[i]) return i; // Found itif (K > A[i]) l = i; // In right half}return A.length; // Search value not in A

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

Saved successfully!

Ooh no, something went wrong!