Question: 8 . Consider the following class. / * * A class that sorts an array of Integer objects from * largest to smallest using a

8. Consider the following class.
/** A class that sorts an array of Integer objects from
* largest to smallest using a selection sort.
*/
public class Sorter
{
private Integer[] a;
public Sorter(Integer[] arr)
{ a = arr; }
/** Swap a[i] and a[j] in array a.*/
private void swap(int i, int j)
{/* implementation not shown */}
/** Sort array a from largest to smallest using selection sort.
* Precondition: a is an array of Integer objects.
*/
public void selectionSort()
{
for (int i =0; i < a.length -1; i++)
{
//find max element in a[i+1] to a[n-1]
Integer max = a[i];
int maxPos = i;
for (int j = i +1; j < a.length; j++)
if (max.compareTo(a[j])<0)//max less than a[j]
{
max = a[j];
maxPos = j;
}
swap(i, maxPos); //swap a[i] and a[maxPos]
}
}
}
If an array of Integer contains the following elements, what would the array
look like after the third pass of selectionSort, sorting from high to low?
8942313109702
(A)1098970134232
(B)1098970421323
(C)1098970321342
(D)8942133109702
(E)1098942313702

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!