I need to write a recursive function that returns the largest and smallest elements in the ArrayList and the corresponding indices. To return these four items, I need to return an object that is an instance of an inner class called MinMaxObject that has four defined private variables: max, min, maxPos, minPos of types double, double, int and int.
I've gotten to here on my own and I need to start the recursion but I have no idea how to get going. If someone could point me in the right direction I should be able to pick it up.
public static void main(String args[]) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter a file name");
String fileName = console.next();
try {
File fileRef = new File(fileName);
Scanner tokens = new Scanner(fileRef);
double input = tokens.nextDouble();
ArrayList<Double> list = new ArrayList<Double>();
while (tokens.hasNextLine()) {
list.add(input);
}
System.out.println("For Loop");
for (int counter = 0; counter < list.size(); counter++) {
System.out.println(list.get(counter));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 2 functions, one initialize & one to call the original recursion
//can start at beggining and recurse to the end or vice versa
//updates the min and max as it goes
//update minPos and maxPos as well
}
public class MinMaxObject {
private double min;
private double max;
private int minPos;
private int maxPos;
public MinMaxObject(double newMin, double newMax, int newMinPos, int newMaxPos){
min = newMin;
max = newMax;
minPos = newMinPos;
maxPos = newMaxPos;
}
public double getMin(){
return min;
}
public void setMin(double newMin){
min = newMin;
}
public double getMax(){
return max;
}
public void setMax(double newMax){
max = newMax;
}
public int getMinPos(){
return minPos;
}
public void setMinPos(int newMinPos){
minPos = newMinPos;
}
public int getMaxPos(){
return maxPos;
}
public void setMaxPos(int newMaxPos){
maxPos = newMaxPos;
}
It sounds like recursion is a part of the assignment.
The simplest way is to iterate through the arraylist, but since you need recursion it's a little more complex. Since this is homework, and because I'm lazy, I'm not going to give you compilable Java code. This is a close approximation. I'm also not going to give you the whole method, cause you should figure the rest out from here.
private int min = 0;
private int max = 0;
findMinMax(int index, List<int> list)
{
//in recursion always start your method with a base-case
if(index >= list.Count)
return ;
//find min and max here
//this is called tail recursion, it's basically the same as a loop
findMinMax(index + 1, list);
}
Thanks @ScubaSteve! ...I outline little more/detailed:
void findMinMax(int idx, List items, MinMaxObject result) {
// this is good:
if (idx >= items.size()) {
return;
}
// check whether list[idx] is min or max: compare with & store to 'result'
// recursion:
findMinMax(idx + 1, items, result);
}
The initial/final/outer invocation would be:
// initialize with high min, low max and "out of range" indices
// ...alternatively in default constructor/field declaration.
MinMaxObject result = new MinMaxObject(Integer.MAX_VALUE, Integer.MIN_VALUE, -1, -1);
// start recursion at index 0:
findMinMax(0, items, result);
A recursion alternative would be: To recourse not only the "head" of the list (idx : 0 ... size - 1), but also the "tail" like:
void findMinMax(int head, int tail, List items, MinMaxObject result) {
if(head > tail) {
// done!
return;
}
// check items[head] and items[tail]... store to result
// iterate:
return findMinMax(head + 1, tail - 1, items, result);
}
the "alternative" halves the count of recursions (but doubles the "inline complexity").
Another "divide & conquer" approach:
void findMinMax(int left, int right, items, result) {
if(left > right) {
return;// alternatively do nothing
} else if (left == right) {
//check items[left] ...
// return;
} else { // left < right
int mid = (right - left) / 2;
findMinMax(left, mid, items, result);
findMinMax(mid + 1, right, items, result);
// return;
}
}
...with (same) O(n) complexity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With