Question
CSC 241 Lab 3 Given an unsorted ArrayList of Integer values, write a recursive method called smallestNumber that will find the smallest number within the ArrayList. The approach should use the fact that the smallest number in a subarray of the ArrayList is equal to the smaller of the first number in the subarray and the smallest number in the remainder of the subarray. The subarray will always contain the last item in the ArrayList. The method will take one parameter-the starting index of the subarray; it will also return the smallest number found. Create and test the smallestNumber method in a driver program given the following array -3,5, 1,-9,8, 16, 10, 4, 5,2,-1
Explanation / Answer
import java.util.ArrayList; public class SmallestNumber { public static int smallestNumber(ArrayList list, int index) { if(index == list.size() - 1) { return list.get(index); } else { int min = smallestNumber(list, index+1); if(list.get(index)