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

Create successful ePaper yourself

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

nonrecursive part of each call. Moreover, we can also see that the memory space<br />

used by the algorithm (<strong>in</strong> addition to the array A) is also roughly proportional to n,<br />

s<strong>in</strong>ce we need a constant amount of memory space for each of the n boxes <strong>in</strong> the<br />

trace at the time we make the f<strong>in</strong>al recursive call (for n = 1).<br />

Revers<strong>in</strong>g an Array by Recursion<br />

Next, let us consider the problem of revers<strong>in</strong>g the n elements of an array, A, so<br />

that the first element becomes the last, the second element becomes second to the<br />

last, <strong>and</strong> so on. We can solve this problem us<strong>in</strong>g l<strong>in</strong>ear recursion, by observ<strong>in</strong>g<br />

that the reversal of an array can be achieved by swapp<strong>in</strong>g the first <strong>and</strong> last<br />

elements <strong>and</strong> then recursively revers<strong>in</strong>g the rema<strong>in</strong><strong>in</strong>g elements <strong>in</strong> the array. We<br />

describe the details of this algorithm <strong>in</strong> Code Fragment 3.32, us<strong>in</strong>g the convention<br />

that the first time we call this algorithm we do so as ReverseArray(A,0,n − 1).<br />

Code Fragment 3.32: Revers<strong>in</strong>g the elements of an<br />

array us<strong>in</strong>g l<strong>in</strong>ear recursion.<br />

Note that, <strong>in</strong> this algorithm, we actually have two base cases, namely, when i = j<br />

<strong>and</strong> when i > j. Moreover, <strong>in</strong> either case, we simply term<strong>in</strong>ate the algorithm, s<strong>in</strong>ce<br />

a sequence with zero elements or one element is trivially equal to its reversal.<br />

Furthermore, note that <strong>in</strong> the recursive step we are guaranteed to make progress<br />

towards one of these two base cases. If n is odd, we will eventually reach the i = j<br />

case, <strong>and</strong> if n is even, we will eventually reach the i > j case. The above argument<br />

immediately implies that the recursive algorithm of Code Fragment 3.32 is<br />

guaranteed to term<strong>in</strong>ate.<br />

Def<strong>in</strong><strong>in</strong>g Problems <strong>in</strong> Ways That Facilitate Recursion<br />

To design a recursive algorithm for a given problem, it is useful to th<strong>in</strong>k of the<br />

different ways we can subdivide this problem to def<strong>in</strong>e problems that have the<br />

same general structure as the orig<strong>in</strong>al problem. This process sometimes means we<br />

need to redef<strong>in</strong>e the orig<strong>in</strong>al problem to facilitate similar-look<strong>in</strong>g subproblems.<br />

For example, with the ReverseArray algorithm, we added the parameters i <strong>and</strong><br />

j so that a recursive call to reverse the <strong>in</strong>ner part of the array A would have the<br />

same structure (<strong>and</strong> same syntax) as the call to reverse all of A. Then, rather than<br />

197

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

Saved successfully!

Ooh no, something went wrong!