//Program in Java please public class SelectionSorter { //Returns the index of t
ID: 3828528 • Letter: #
Question
//Program in Java please
public class SelectionSorter {
//Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex.
public static int findMax(int fromIndex, Integer[] arrayOfIntegers) {
throw new UnsupportedOperationException("you must implement this method");
//Performs the standard "insertion sort" on a shallow copy of the incoming array.
public static Integer[] insertionSort(Integer[] incoming) {
throw new UnsupportedOperationException("you must implement this method");
Explanation / Answer
//Program in Java please
public class SelectionSorter {
//Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex.
public static int findMax(int fromIndex, Integer[] arrayOfIntegers) {
if (fromIndex >= arrayOfIntegers.length)
{
return -1;
}
Integer max = arrayOfIntegers[fromIndex];
int index = fromIndex;
for(int i = fromIndex+1; i < arrayOfIntegers.length; i++)
{
if(max < arrayOfIntegers[i])
{
max = arrayOfIntegers[i];
index = i;
}
}
return index;
}
//Performs the standard "insertion sort" on a shallow copy of the incoming array.
public static Integer[] insertionSort(Integer[] incoming) {
Integer[] ret = new Integer[incoming.length];
for (int i = 0; i < incoming.length; i++) {
ret[i] = incoming[i];
}
int i, key, j;
for (i = 1; i < ret.length; i++)
{
key = ret[i];
j = i-1;
while (j >= 0 && ret[j] > key)
{
ret[j+1] = ret[j];
j = j-1;
}
ret[j+1] = key;
}
return ret;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.