7,119 views
0 votes
0 votes
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.

}
}

User Rsalinas
by
3.4k points

2 Answers

3 votes
3 votes

Final answer:

To implement the descending selection sort algorithm, you can use a nested loop. The outer loop iterates from the first element to the second-to-last element, and the inner loop iterates from the element after the outer loop's iteration variable to the last element. In each iteration of the outer loop, you find the maximum element in the remaining unsorted portion of the array and swap it with the element at the current outer loop's iteration variable. After each iteration of the outer loop, you output the array.

Step-by-step explanation:

To implement the descending selection sort algorithm, you can use a nested loop. The outer loop iterates from the first element to the second-to-last element, and the inner loop iterates from the element after the outer loop's iteration variable to the last element. In each iteration of the outer loop, you find the maximum element in the remaining unsorted portion of the array and swap it with the element at the current outer loop's iteration variable. After each iteration of the outer loop, you output the array.

Here's an example implementation:

public static void selectionSortDescendTrace(int [] numbers, int numElements) {
for (int i = 0; i < numElements - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < numElements; j++) {
if (numbers[j] > numbers[maxIndex]) {
maxIndex = j;
}
}
int temp = numbers[i];
numbers[i] = numbers[maxIndex];
numbers[maxIndex] = temp;
for (int k = 0; k < numElements; k++) {
System.out.print(numbers[k] + " ");
}
System.out.println();
}
}

In the main method, you can use a loop to read the input integers and store them in the numbers array. After that, call the selectionSortDescendTrace() method with the numbers array and the number of elements read as arguments.

User Zarel
by
3.2k points
5 votes
5 votes

Final answer:

The DescendingOrder class needs a selectionSortDescendTrace method that sorts an integer array in descending order and prints the array after each outer loop iteration. In the main method, the code reads up to 10 integers, ends with -1, and invokes the sorting method.

Step-by-step explanation:

The student is tasked with implementing a selection sort algorithm in Java that sorts an array into descending order and prints the array after each pass of the outer loop. The method selectionSortDescendTrace must be written within the DescendingOrder class. In main(), a list of up to 10 positive integers is to be read in and processed by the sorting method, with -1 signaling the end of input.

Implementation of selectionSortDescendTrace Method:

Below is a suitable implementation for the requested method:

public static void selectionSortDescendTrace(int [] numbers, int numElements) {
for (int i = 0; i < numElements - 1; i++) {
// Find index of the maximum element
int indexMax = i;
for (int j = i + 1; j < numElements; j++) {
if (numbers[j] > numbers[indexMax]) {
indexMax = j;
}
}
// Swap the found maximum element with the first element
int temp = numbers[i];
numbers[i] = numbers[indexMax];
numbers[indexMax] = temp;

// Printing the current state of the array
for (int k = 0; k < numElements; k++) {
System.out.print(numbers[k] + " ");
}
System.out.println();
}
}

Modification of the main Method:

To read the integers and call the sorting function, the main method could look like this:

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int input, i = 0;
int[] numbers = new int[10];

while (i < 10 && (input = scnr.nextInt()) != -1) {
numbers[i++] = input;
}

selectionSortDescendTrace(numbers, i);
}

This code will take user input, store it into an array, and then call the sorting algorithm to sort and print the array in descending order.

User Rashiem
by
3.4k points