Question

In: Computer Science

You will build some methods that search for the smallest element and the index of the...

You will build some methods that search for the smallest element and the index of the smallest element in an array in a recursive fashion. I know you can already do this without recursion. The point is to use recursion, so use it.

public class Driver
{
   public static void main(String[] args)
   {
       int[] one = {2, 4, 6, 1, 6, 3, 8};
       int[] two = {43, 76, 3, 23, 95, 23};
       int[] three = {9, 8, 7, 6, 5, 4, 3, 2, 1};
       
       Recursion.print(one);
       System.out.println("Smallest: " + Recursion.smallest(one));
       System.out.println("Index of Smallest: " + Recursion.smallestIndex(one));
       System.out.println();
       
       Recursion.print(two);
       System.out.println("Smallest: " + Recursion.smallest(two));
       System.out.println("Index of Smallest: " + Recursion.smallestIndex(two));
       System.out.println();
       
       Recursion.print(three);
       System.out.println("Smallest: " + Recursion.smallest(three));
       System.out.println("Index of Smallest: " + Recursion.smallestIndex(three));
       System.out.println();
    }
}
public class Recursion
{
    public static void print(int[] array)
    {
        // Your code goes here.
    }
    
    public static int smallest(int[] array)
    {
        return smallestFrom(array, 0);
    }
    
    private static int smallestFrom(int[] array, int start)
    {
        // Your code goes here.
    }
    
    public static int smallestIndex(int[] array)
    {
        return smallestIndexFrom(array, 0);
    }
    
    private static int smallestIndexFrom(int[] array, int start)
    {
        // Your code goes here.
    }
}

Output

2,4,6,1,6,3,8
Smallest: 1
Index of Smallest: 3

43,76,3,23,95,23
Smallest: 3
Index of Smallest: 2

9,8,7,6,5,4,3,2,1
Smallest: 1
Index of Smallest: 8

Why are smallestFrom() and smallestIndexFrom() private? Should they be?

Why are all the methods in Recursion static? Could it be changed so they are not? Which one is better for this application?

Solutions

Expert Solution

code

========================

public class Driver
{
public static void main(String[] args){
int[] one = {2, 4, 6, 1, 6, 3, 8};
int[] two = {43, 76, 3, 23, 95, 23};
int[] three = {9, 8, 7, 6, 5, 4, 3, 2, 1};

Recursion.print(one);
System.out.println("Smallest: " + Recursion.smallest(one));
System.out.println("Index of Smallest: " + Recursion.smallestIndex(one));
System.out.println();

Recursion.print(two);
System.out.println("Smallest: " + Recursion.smallest(two));
System.out.println("Index of Smallest: " + Recursion.smallestIndex(two));
System.out.println();

Recursion.print(three);
System.out.println("Smallest: " + Recursion.smallest(three));
System.out.println("Index of Smallest: " + Recursion.smallestIndex(three));
System.out.println();
}
}

public class Recursion
{
public static void print(int[] array)
{
for(int i=0;i>array.length;i++)
           System.out.print(array[i]+" ");
}
  
public static int smallest(int[] array)
{
return smallestFrom(array, 0);
}
  
private static int smallestFrom(int[] array, int start)
{
       if (start == array.length - 1) {
           return array[start];
}

       double val = min(array, start + 1);

       if (array[start] < val)
           return array[start];
       else
           return val;
   }
  
public static int smallestIndex(int[] array)
{
return smallestIndexFrom(array, 0);
}
  
private static int smallestIndexFrom(int[] array, int start)
{
       if (start == array.length - 1) {
           return start;
}

       int index = min(array, start + 1);

       if (array[start] < array[index])
           return start;
       else
           return index;
      
}
}


Related Solutions

Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
Using the trend in the periodic table, and position of an element in it, the smallest element is
Using the trend in the periodic table, and position of an element in it, the smallest element is a. hydrogen b. Helium c. Oxygen d. Carbon
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT