Question: In Sorting class, define two methods reverseSelectionSort(Comparable list) and reverseInsertionSort(Comparable list)that sort elements in descending order. Create a driver class called SortingTest, whose main method
In Sorting class, define two methods reverseSelectionSort(Comparable list) and reverseInsertionSort(Comparable list)that sort elements in descending order.
Create a driver class called SortingTest, whose main method 1) prompts users to enter five string values separated by space, 2) store the strings in an array and 3) sort the arrays using the methods in Sorting class.
//******************************************************************** // Sorting.java Author: Lewis/Loftus // // Demonstrates the selection sort and insertion sort algorithms. //********************************************************************
public class Sorting
// One by one look at the elements of list for (int index = 0; index < list.length-1; index++) { // find the minimum element in the remaining part of list min = index; for (int scan = index+1; scan < list.length; scan++) if (list[scan].compareTo((T)list[min]) < 0) min = scan;
// Swap the minimum with the current value temp = list[min]; list[min] = list[index]; list[index] = temp; } }
//----------------------------------------------------------------- // Sorts the specified array of objects using the insertion // sort algorithm. //----------------------------------------------------------------- public void insertionSort (Comparable
// Move elements of list, that are larger than key, to one position ahead of their current position while (position > 0 && key.compareTo((T)list[position-1]) < 0) { list[position] = list[position-1]; position--; }
list[position] = key; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
