Question: 15) Consider the sort method for selection sort shown below: public static void sort (int[] a) { for (int i = 0; i < a.length

15) Consider the sort method for selection sort shown below:

 
public static void sort (int[] a) 
{ 
 for (int i = 0; i < a.length  1; i++) 
 { 
 int minPos = minimumPosition(i); 
 swap(minPos, i); 
 } 
} 

Suppose we modify the loop control to read int i = 1; i < a.length 1; i++. What would be the result?

a) An exception would occur

b) The sort would not consider the last array element.

c) The sort would not consider the first array element.

d) The sort would still work correctly.

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.

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++) 
 { 
 ________________ 
 } 
 return maxPos; 
} 

a) if(a[i] > a[maxPos]) { maxPos = i; }

b) if(a[i] == a[maxPos]) { maxPos = i; }

c) if(a[i] < a[maxPos]) { maxPos = i; }

d) if(a[i] <= a[maxPos]) { maxPos = i; }

17) The merge sort algorithm presented in section 14.4, which sorts an array of integers in ascending order, uses the merge method which is partially shown below. Select the condition that would be needed to complete the method so that the elements are sorted in descending order.

private static void merge(int[] first, int[] second, int[] a) 
{ 
 int iFirst = 0; 
 int iSecond = 0; 
 int j = 0; 
 
 while (iFirst < first.length && iSecond < second.length) 
 { 
 if (_____________________________) 
 { 
 a[j] = first[iFirst]; 
 iFirst++; 
 } 
 else 
 { 
 a[j] = second[iSecond]; 
 iSecond++; 
 } 
 j++; 
 } 
 // rest of the method follows here 

}

a) first[iFirst] < second[iSecond]

b) iFirst < iSecond

c) first[iFirst] > second[iSecond]

d) iFirst > iSecond

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!