I would really appreciate it if someone can help me this this assignment. Please
ID: 3709803 • Letter: I
Question
I would really appreciate it if someone can help me this this assignment. Please provide comments on your code so I can understand the logic behind it. I will give you a thumbs up for your assistantce. Thank You
Programming Assignment #7
(Recursion)
This assignment is to write some methods that perform simple array operations recursively.
Specifically, you will write the bodies for the 3 recursive methods of the ArrayRecursionclass, available on the class web page.
No credit will be given if any changes are made to ArrayRecursion.java, other than completing the three methods
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 sortalgorithm and the recursiveGetIndexOfLargest method you wrote in 2.
Find the largest value in the array and swap it with the last element. Then, repeat the process for that portion of the array containing all but the last element.
No credit will be given for any method that uses a loop. Loops are not necessary and if you use one you are missing the point of the assignment
BELOW IS THE CODE THATS GOING TO BE USED
-----------------------------------------------------------------------
/* File: ArrayRecursion.java
*
* Programmer: your name
*
*/
import java.util.Random;
public 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)
{
}
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);
}
}
Explanation / Answer
Hi Dear,
Please find my implementation:
Output:
Original array: 24 75 65 72 48 23 68 45 47 66
Reversed array: 66 47 45 68 23 48 72 65 75 24
Index of the Largest Value in the array: 8
Sorted array: 23 24 45 47 48 65 66 68 72 75
Process finished with exit code 0