Asked by Tyler Schmitt on Jun 18, 2024

verifed

Verified

Write a Java method as well as any facilitator methods needed to perform a sort on an array of whole numbers in descending order.

Whole Numbers

Numbers without fractions; including all positive integers, zero, and their negatives, also referred to as integers.

Descending Order

A sequence arranged from largest to smallest or highest to lowest.

Sort

A process or function in computer science to arrange data in a specified order, such as ascending or descending.

  • Implement sorting algorithms on arrays.
verifed

Verified Answer

KG
Kabir GandhiwadiJun 22, 2024
Final Answer :
/**
Precondition: The array has values.
Action: Sorts a so that a[0] >= a[1] >= ... >= a[a.length]
*/
public static void selectionSortint[] a)
{
int indexOfLargest;
forint i=0;i < a.length;++i)
{
indexOfLargest = LargestIndexi,a);
swapi,indexOfLargest,a);
}
}
/**
Returns the index of the largest value among
a[start],a[start + 1],...a[a.length-1]
*/
private static int LargestIndexint start,int[] a)
{
int max = a[start];
int indexOfMax = start;
forint i=start + 1;i < a.length;i++)
{
ifa[i] > max)
{
max = a[i];
indexOfMax = i;
}
}
return indexOfMax;
}
/**
Precondition: i and j are legal indices for the array a.
Postcondition: Values of a[i] and a[j] have been interchanged
*/
private static void swapint i,int j,int[] a)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}