Question: (Recursion) This assignment is to write some methods that perform simple array operations recursively. For this assignment you will write the bodies for 3 recursive
(Recursion)
This assignment is to write some methods that perform simple array operations recursively.
For this assignment you will write the bodies for 3 recursive methods of the ArrayRecursion class, available on the class web page in file ArrayRecursionTest.java
You are to make NO other changes to the ArrayRecursion class other than writing the 3 method bodies. Also make no changes to the test class.
Note that the public methods of ArrayRecursion reverse(), sort(), and getIndexOfLargest() cannot be recursive because they have no parameters. Each of these methods calls a private recursive helper method that does the work recursively. You will write the bodies of these recursive helper methods.
Remember that recursion is not merely another way to implement a loop. Recursive algorithms always have one or more base or trivial cases that are solved nonrecursively, and each recursive call must be made for a reduced case of the problem that gets closer to the trivial case. Therefore, to receive credit you must implement the algorithms given below.
reverseRecurse - reverse the order of the values stored in the array, using this algorithm:
Swap the first element value with the last. Then, recursively reverse that portion of the array containing all but the first and last elements
recursiveGetIndexOfLargest - return the index of the largest value stored in the array, using this algorithm:
Find the largest value in that portion of the array containing all but the last element. Compare that value to the value in the last element and return the index of the larger of the two.
recursiveSort sort the array in ascending order, using the selection sort algorithm and the recursiveGetIndexOfLargest method you wrote in 2.
Find the largest value in the array and swap it with the last element. Then, remove the last element and repeat the process for the shortened array.
========================================================================================================
import java.util.Random; class ArrayRecursion { // instance var's private int[] list; // array of ints private int count = 0; // number of elements used /** * Create an ArrayRecursion object. Create an array with between 10 and 15 * elements, and fill it with random positive 2-digit ints */ public ArrayRecursion() { Random r = new Random(); count = r.nextInt(6) + 10; list = new int[count]; for (int i = 0; i < count; i++) { list[i] = r.nextInt(90) + 10; } } /* * Return the list as a string * @return a string containing all ints stored in list */ public String toString() { String out = ""; for (int i = 0; i < count; i++) { out += list[i] + " "; } return out + " "; } /** * Reverse the order of the values stored in the list. (called by client to * reverse list using first algorithm) */ public void reverse() { reverseRecurse(list, 0, count); } // recursive "helper" method to reverse the values stored in the list // (called by public method reverse1()) private void reverseRecurse(int[] list, int start, int count) { } /* * Returns the index of the largest value in the array. * @return the index of the largest value in the array */ public int getIndexOfLargest() { return recursiveGetIndexOfLargest(list, count); } // recursive "helper" method to return index of largest value // (called by public method getLargest()) private int recursiveGetIndexOfLargest(int[] list, int count) { return -999; // bogus value to enable program skeleton to run } /* * Sort the array in ascending order using the selection sort */ public void sort() { recursiveSort(list, count); } // recursive "helper" method to sort the array // (called by public method sort()) private void recursiveSort(int[] list, int count) { } } /** * A test class for the ArrayRecursion class */ public class ArrayRecursionTest { public static void main(String[] args) { ArrayRecursion list = new ArrayRecursion(); System.out.println(" Original: " + list); list.reverse(); System.out.println(" Reversed: " + list); System.out.println("Largest value is at index: " + list.getIndexOfLargest()); list.sort(); System.out.println(" Sorted: " + list); } }
=======================================
All code must be well commented for full credit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
