Descending selection sort with output during execution Write a void method selection SortDescend Trace() that takes an integer array, and sorts the array into descending order. The method should use nested loops and output the array after each iteration of the outer loop, thus outputting the array N-1 times (where N is the size). Complete the main() to read in a list of up to 10 positive integers (ending in -1) and then call the selection SortDescendTrace() method. If the input is: 20 10 30 40 -1 then the output is: 40 10 30 20 40 30 10 20 40 30 20 10 LAB ACTIVITY 7.10.1: LAB: Descending selection sort with output during execution 0/10 DescendingOrder.java

import java.util.Scanner;

public class DescendingOrder {
// TODO: Write a void method selectionSortDescendTrace() that takes
// an integer array and the number of elements in the array as arguments,
// and sorts the array into descending order.
public static void selectionSortDescendTrace(int [] numbers, int numElements) {

}


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

int input, i = 0;
int numElements = 0;
int [] numbers = new int[10];

// TODO: Read in a list of up to 10 positive integers; stop when
// -1 is read. Then call selectionSortDescendTrace() method.

}
}

Respuesta :

Answer:

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class DescendingOrder {
  4.    // TODO: Write a void method selectionSortDescendTrace() that takes
  5. // an integer array and the number of elements in the array as arguments,
  6. // and sorts the array into descending order.
  7.    public static void selectionSortDescendTrace(int [] numbers, int numElements) {
  8.        for(int i = 0; i < numbers.length-1; i++){
  9.            int currentMax = numbers[i];
  10.            int index = i;
  11.            for(int j = i + 1; j < numbers.length; j++){
  12.                if(numbers[j] > currentMax){
  13.                    currentMax = numbers[j];
  14.                    index = j;
  15.                }
  16.            }
  17.            int temp = numbers[i];
  18.            numbers[i] = numbers[index];
  19.            numbers[index] = temp;
  20.            System.out.println(Arrays.toString(numbers));
  21.        }
  22.    }
  23.    public static void main(String[] args) {
  24.        Scanner scnr = new Scanner(System.in);
  25.        int input, i = 0;
  26.        int numElements = 0;
  27.        int [] numbers = new int[10];
  28. // TODO: Read in a list of up to 10 positive integers; stop when
  29. // -1 is read. Then call selectionSortDescendTrace() method.
  30.        do{
  31.            input = scnr.nextInt();
  32.            if(input != -1){
  33.                numbers[i] = input;
  34.                i++;
  35.                numElements++;
  36.            }
  37.        }while(input != -1);
  38.        selectionSortDescendTrace(numbers, numElements);
  39.    }
  40. }

Explanation:

Firstly, create a do while loop to repeatedly to prompt user for number (Line 38 - 46).

Next, proceed to work on selectionSortDescendTrace method. Create a nested loop to implement selection sort logic. In outer loop, create variable currentMax and index, to track the maximum number and position of the maximum number for each iteration (Line 11 - 12). In inner loop, a current element value from array will be checked against the subsequent elements in array to identify the maximum value of that iteration (Line 13 - 17). Upon exist from the inner loop, the currentMax is swapped with the current array element indexed by i (Line 20 -21).

Print the array to console for every outer loop iteration (Line 24).