Please explain why you chose your answer. 16) Consider the minimumposition metho
ID: 3833187 • Letter: P
Question
Please explain why you chose your answer.
16) Consider the minimumposition method from the selectionsorter class. Complete the code to write a maximumposition method that returns the index of the largest element in the range from index from to the end of the array static int minimum Position (int int from) ate int min Pos from for (int from 1 i a length; it Pos eturn min. ate static int maximum Position (int, a, int from) ant maxPoa from for (int from 1 i a length; it retur max Pos a if a [i] a Imax Posl max Pos b) if a [i] a Imax Pos] max Pos if a [i] a Imax Posl max Pos d) if (a il a Imax Pos] max PosExplanation / Answer
16. private static int minimumPosition(int[] a, int from)
{
int minPos = from;
for(int i = from+1; i < a.length; i++)
if(a[i] < a[minPos])
minPos = i;
return minPos;
}
private static int maximumPosition(int[] a, int from)
{
int maxPos = from;
for(int i = from+1; i < a.length; i++) //For each element starting from index to last element.
if(a[i] > a[maxPos]) //If that element is larger than the previously calculated largest position.
maxPos = i; //Update the maxPos.
return maxPos;
}
So, the answer is: a.
17.
private static void merge(int[] first, int[] second, int[] a)
{
int iFirst = 0; //The start position of first array.
int iSecond = 0; //The start position of second array.
int j = 0; //The start position of third array a.
while(iFirst < first.length && iSecond < second.length) //As long as there are elements in both the arrays.
{
if(first[iFirst] < second[iSecond]) //If the element in first array is less than element in second array.
{
a[j] = first[iFirst]; //Copy the least value to third array.
iFirst++;
}
}
}
So, the answer is: a.
18.
Give the following code snippet for searching an array:
int[] arr = {3, 8, 12, 14, 17};
int newVal = 15;
int pos = Arrays.binarySearch(arr, newVal);
What value will pos have when this code is executed?
mid = (0 + 4)/2 = 2.
mid = (3 + 4)/2 = 3.
mid = (4 + 4)/2 = 4.
-4-1 = -5 is returned.
So, the answer is: b.
19.
int index = Collections.binarySearch(bands, "Beatles");
if(index < 0)
bands.add(-1*index, "Beatles"); will insert the value in right position.
So, the answer is: c.