23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

This claim is true at the beg<strong>in</strong>n<strong>in</strong>g of the first iteration of the loop, s<strong>in</strong>ce there are<br />

no elements among the first 0 <strong>in</strong> A (this k<strong>in</strong>d of a trivially true claim is said to<br />

hold vacuously). In iteration i, we compare element x to element A[i] <strong>and</strong> return<br />

the <strong>in</strong>dex i if these two elements are equal, which is clearly correct <strong>and</strong> completes<br />

the algorithm <strong>in</strong> this case. If the two elements x <strong>and</strong> A[i] are not equal, then we<br />

have found one more element not equal to x <strong>and</strong> we <strong>in</strong>crement the <strong>in</strong>dex i. Thus,<br />

the claim S i will be true for this new value of i; hence, it is true at the beg<strong>in</strong>n<strong>in</strong>g<br />

of the next iteration. If the while-loop term<strong>in</strong>ates without ever return<strong>in</strong>g an <strong>in</strong>dex<br />

<strong>in</strong> A, then we have i = n. That is, S n is true—there are no elements of A equal to x.<br />

Therefore, the algorithm correctly returns —1 to <strong>in</strong>dicate that x is not <strong>in</strong> A.<br />

4.4. Exercises<br />

For source code <strong>and</strong> help with exercises, please visit<br />

java.datastructures.net.<br />

Re<strong>in</strong>forcement<br />

R-4.1<br />

Give a pseudo-code description of the O(n)-time algorithm for comput<strong>in</strong>g the<br />

power function p(x, n). Also, draw the recursion trace of this algorithm for the<br />

computation of p(2,5).<br />

R-4.2<br />

Give a <strong>Java</strong> description of Algorithm Power for comput<strong>in</strong>g the power function<br />

p(x, n) (Code Fragment 4.3).<br />

R-4.3<br />

Draw the recursion trace of the Power algorithm (Code Fragment 4.3, which<br />

computes the power function p(x,n)) for comput<strong>in</strong>g p(2,9).<br />

R-4.4<br />

Analyze the runn<strong>in</strong>g time of Algorithm B<strong>in</strong>arySum (Code Fragment 3.34) for<br />

arbitrary values of the <strong>in</strong>put parameter n.<br />

R-4.5<br />

Graph the functions 8n, 4nlogn, 2n 2 , n 3 , <strong>and</strong> 2 n us<strong>in</strong>g a logarithmic scale for the<br />

x- <strong>and</strong> y-axes , that is, if the function value f(n) is y, plot this as a po<strong>in</strong>t with x-<br />

coord<strong>in</strong>ate at log n <strong>and</strong> y-coord<strong>in</strong>ate at log y.<br />

R-4.6<br />

248

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

Saved successfully!

Ooh no, something went wrong!