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

What will be an ideal response?


```
/**
Precondition: The array has values.
Action: Sorts a so that a[0] >= a[1] >= ... >= a[a.length]
*/
public static void selectionSort(int[] a)
{
int indexOfLargest;
for(int i=0; i < a.length; ++i)
{
indexOfLargest = LargestIndex(i, a);
swap(i, indexOfLargest, a);
}
}
/**
Returns the index of the largest value among
a[start], a[start + 1], ... a[a.length-1]
*/
private static int LargestIndex(int start, int[] a)
{
int max = a[start];
int indexOfMax = start;
for(int i=start + 1; i < a.length; i++)
{
if(a[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 swap(int i, int j, int[] a)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
```

Computer Science & Information Technology

You might also like to view...

A(n) _________________ is an object that has methods that allow you to process a collection of items one at a time.

a) iterator b) loop c) conditional d) palindrome e) nested loop

Computer Science & Information Technology

A string ________.

a) is only declared using the new keyword b) is immutable c) can dynamically resize d) is written as a series of characters surrounded by double quotation marks e) Both b and d.

Computer Science & Information Technology

In Excel, you can reduce the total width of a worksheet by adjusting the column widths

Indicate whether the statement is true or false

Computer Science & Information Technology

With respect to security, CIA stands for?

A. Confidentiality, integrity, and availability B. Confidentiality, information, and availability C. Confidentiality, integrity, and authentication D. Confidentiality, information, and authorization

Computer Science & Information Technology