In: Computer Science
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?
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;
}
}