In: Computer Science
Please use java language in an easy way and comment as much as you can! Thanks
(Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
(Analysis of Algorithms question) Determine the growth function and time complexity (in Big-Oh notation) of the findMin method from the previous problem. Please show your work by annotating the code.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.*;
class Main
{
//Returns T , the generic type
public static <T extends Comparable<? super T>> T
findMin(T[] array)
{
// take the first element as the minimum element
T min=array[0];
//Loop through the list
for(T element:array)
{
//check if the element is minimum
if(element.compareTo(min)<0)
min=element;
}
//return the output
return min;
}
public static void main (String[] args)
{
//Check with Integer objects
Integer[] array={100,2,3,49,5};
System.out.println(findMin(array));
//Check with String objects
String[] arr={"Hai", "Hello","abcd","A"};
System.out.println(findMin(arr));
}
}
====================