15.3 Running Time Analysis of Algorithms and Impact of Coding: Evaluating Recursive Methods
In this section, we will learn how to compute the running time of a recursive method. We will also look at how coding a method has a direct impact on its running time.
Consider coding a recursive method that takes one parameter, n, and returns 2n. There are several ways to code that method, and we will consider two of them here so that we can assess which algorithm is more efficient.
Our first method, powerOf2A, is designed using this approach:
when n = 0, 20 = 1. This is our base case.
For our general case, we use this calculation: 2n = 2 * 2n – 1
This first problem formulation results in the method shown in Example 15.4.
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.