import java.util.*; /**** * * This class illustrates how to compute the sum of an array recursively. The * public sum method takes an array of integers and returns the sum of all its * elements. A private "helper" creates a sub-array using methods from the * Java library. * * In contrast to the helper method in ./RecursiveSum.java, q.v., the helper in * this class creates a sub-array directly rather than using the more typical * technique of passing a full array with an int position. * * Even with judicious use of Java library methods, there is an O(n) penalty * for the kind of sub-array helper used here. In addition, the sum method * must take an array of Integer, not of primitive int. The conclusion, as * noted in class, is that traversing array and list data structures is * better done iteratively than recursively in Java. * * There are indeed languages where sublist creation is a more natural and * efficient part of the language repertoire, and where recursion is the norm * rather than iteration. Studying such languages is a bit of a mind-altering * experience when coming at them from Java and C. You may look forward to * such mind alteration in future computer science classes. * */ public class RecursiveSumAlternative { /** * Return the sum of the given array. Return 0 for an empty array. Assume * the array is not null. */ public int sum(Integer a[]) { /** * Base Case: Return a sum of 0 if the array is empty. */ if (a.length == 0) return 0; /** * Recursive Step: Return the sum of the first element of the array * with the recursive sum of the rest of the array. The first element * is at a[0]. The rest of the array is returned by the rest method. */ return a[0] + sum(rest(a)); } /** * Return the rest of the given array. This is defined as the array with * all the elements of the given array except the first, i.e., the elements * at positions a[1] through a[a.length-1]. */ Integer[] rest(Integer a[]) { /* * Be very careful here. Among other things, using the array a as the * argument of toArray mutates that array in a very nasty way. I was * hoping to avoid creation of a new array, so this hack would stay * O(1). However, I don't believe it is possible to implement this * method with O(1) behavior using the tools available in Java. */ return Arrays.asList(a).subList(1,a.length).toArray(new Integer[0]); /* * The following O(n) code is almost certainly faster than the * preceding set of library calls. :( * Integer[] rest = new Integer[a.length-1]; for (int i = 0; i < a.length-1; i++) rest[i] = a[i+1]; return rest; * */ } }