Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sorting Algorithms [60 points] In Sorting class, define two methods reverseSelectionSort(Comparable list) and reverseInsertionSort(Comparable list)that sort elements in descending order. Create a driver class called

Sorting Algorithms [60 points]

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.

Sample Run:

Enter five string values: paper true soap floppy flower

Using Selection Sort to Sort Strings in Ascending Order floppy, flower, paper, soap, true

Using Selection Sort to Sort Strings in Descending Order true, soup, paper, flower, floppy

Using Insertion Sort to Sort Strings in Ascending Order floppy, flower, paper, soap, true

Using Insertion Sort to Sort Strings in Descending Order true, soup, paper, flower, floppy

This is the starting code I have been given

//******************************************************************** // Sorting.java Author: Lewis/Loftus // // Demonstrates the selection sort and insertion sort algorithms. //********************************************************************

public class Sorting { //----------------------------------------------------------------- // Sorts the specified array of objects using the selection // sort algorithm. //----------------------------------------------------------------- public void selectionSort(Comparable[] list) { int min; Comparable temp;

// 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[] list) { for (int index = 1; index < list.length; index++) { Comparable key = list[index]; int position = index;

// 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; } } }

If someone could help that would be great!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Optimization And Data Science Trends And Applications 5th Airoyoung Workshop And Airo Phd School 2021 Joint Event

Authors: Adriano Masone ,Veronica Dal Sasso ,Valentina Morandi

1st Edition

3030862887, 978-3030862886

More Books

Students also viewed these Databases questions

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago

Question

b. Why were these values considered important?

Answered: 1 week ago