Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help with completing code for JAVA using intellij IDEA. Need to write a divide and conquer generic function to find the smallest element in
Please help with completing code for JAVA using intellij IDEA.
Need to write a divide and conquer generic function to find the smallest element in an array.
Code should split the array in half and recursively find the smallest of each side, then return the smallest of the two values.
The program should count the number of comparisons made and output it to the screen.
Here is the program outline so far:
import java.util.ArrayList; import java.util.*; public class HW13 { /** counter for the number of comparisons made inside the method smallest() */ static int comparisonCount=0; /** * Finds the smallest value in an array of Comparable objects * @param list the array to be searched of type* @param start starting index to search for the smallest * @param stop stoping index to search for the smallest * @return the smallest value of type * * This function recursively divides the array in half and findest the smallest value in each half. * It then returns the smallest of the two values. */ public static > E smallest(E[] list,int start, int stop) { // Your code HERE } /** * The initial call to smallest which calls the recursive version * @param list the array of type to be searched * @return the smallest value in list * * This routine also displays the number of comparisons made inside the recursive smallest() call. */ public static > E smallest(E[] list) { comparisonCount=0; E result=smallest(list,0,list.length-1); System.out.printf("\tSearching %d numbers used %d comparisons ",list.length,comparisonCount); return result; } public static void main (String s[]) { Integer iArray[]={10,11,20,-5,23,64,210,8,9,20,19,5,-94,-5,56,23}; Double dArray[]={19.2,1.1,2.5,-291.3,-312.1,81.23,99.57}; Long lArray[]={10L,200L,-124L,300L,123L,-39L,1234567890000000L,2L,3L,6L,9L,21L,24L}; Integer trouble[] ={1001}; System.out.printf("Smallest of %s is %d ",Arrays.toString(iArray),smallest(iArray)); System.out.printf("Smallest of %s is %f ",Arrays.toString(dArray),smallest(dArray)); System.out.printf("Smallest of %s is %d ",Arrays.toString(lArray),smallest(lArray)); System.out.printf("Smallest of %s is %d ",Arrays.toString(trouble),smallest(trouble)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started