Question

Question: public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should...

Question:

public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively. Your method may not contain any while or for loops. Hint: the public findMinRecursive method should not, itself, be recursive. Instead, write an additional private helper method with additional parameters. This private helper method should be called from findMinRecursive. This must run in O(n) time.

Here is findMin:

public static <T extends Comparable<T>> int findMin(T[] arr) {
int index = 0;
   T min = arr[index];
  
   for (int i = 1; i < arr.length; i++) {
       if (arr[i].compareTo(min) < 0) {
           min = arr[i];
           index = i;
       }
       if (arr[i].compareTo(min) == 0) {
           min = min;
       }
   }
   return index;
}

This is the code I have right now (main method for testing) - and it just returns zero:

public static <T extends Comparable<T>> int findminRecursive(T[] arr) {
   int index = 0;
   int i = 1;
   T min = arr[0];
   int minvalue = helper(index, i, min, arr);
   return minvalue;
}
  
   public static <T extends Comparable<T>> int helper(int index, int i, T min, T[] arr) {
      
       if (i < arr.length && arr[i].compareTo(min) < 0) {
           min = arr[i];
           index = i;
           i++;
           helper(index, i, min, arr);
       }
       else if (i < arr.length && arr[i].compareTo(min) == 0) {
           min = min;
           i++;
           helper(index, i, min, arr);
       }
       else if (i < arr.length && arr[i].compareTo(min) > 0) {
           min = min;
           i++;
           helper(index, i, min, arr);
       }
      
       return index;
   }


public static void main(String[] args) {
Integer[] test = {7,90,10,2,15,23,3,217,3,1};
System.out.println(findminRecursive(test));

}

Ïm stuck... Thanks!

Homework Answers

Answer #1
public class FindMinRecursive {

    public static <T extends Comparable<T>> T findminRecursive(T[] arr) {
        return helper(arr, arr.length);
    }

    public static <T extends Comparable<T>> T helper(T[] arr, int size) {
        if (size == 1) {
            return arr[0];
        } else {
            T min = helper(arr, size - 1);
            if (arr[size - 1].compareTo(min) < 0) {
                min = arr[size - 1];
            }
            return min;
        }
    }

    public static void main(String[] args) {
        Integer[] test = {7, 90, 10, 2, 15, 23, 3, 217, 3, 1};
        System.out.println(findminRecursive(test));
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
Language: Java Topic: Heaps (Min Heaps) Main Code: public class MinHeap<T extends Comparable<? super T>> {...
Language: Java Topic: Heaps (Min Heaps) Main Code: public class MinHeap<T extends Comparable<? super T>> { /** * The initial capacity of the MinHeap when created with the default * constructor. * * DO NOT MODIFY THIS VARIABLE! */ public static final int INITIAL_CAPACITY = 13; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int size; QUESTION: Write a method public T getMin() that: * Returns the minimum element in the heap. *...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
Please find the BigO function with explanation for each of the programs below: - Recursive binary...
Please find the BigO function with explanation for each of the programs below: - Recursive binary search - Computing the factorial of N - Computing the N-th Fibonacci number. import java.util.Scanner; public class Recursive { public static void main(String args[]) { int arr[]= {7,9,12,15,28,57,92}; Scanner in = new Scanner(System.in); int key; System.out.print("Enter key to search in array : "); key = in.nextInt(); int index = binarySearch(arr,0,arr.length-1,key); if(index==-1)System.out.println(key + " is not found in array\n"); else System.out.println(key + " is found...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values to an array and the other that uses the insertion method to sort the array. And of course I have a main method. I need to call both of these methods in the main method. I have successfully called the random assigned method from the main method, but I am confused on how to call the second sort method. Here is what I have--...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
What's wrong with this code? #Java Why I am getting error in : int BusCompare =...
What's wrong with this code? #Java Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } } public class Type implements Comparable<Type> { int id; String name; int...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];...
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];       public int hash(int ele){        return ele % this.hashTable.length;    }    public boolean insert(int ele){        int pos = hash(ele);        System.out.println("Hashing key " + pos);        if(this.hashTable[pos] == 0)            this.hashTable[pos] = ele;        else{            for(int i = pos + 1; i < this.hashTable.length; i++){                if(this.hashTable[i]...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT