Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

!!!You will be creating a driver only for this assignment, however, the sorting methods should be entirely outside the main method.You will be creating a

!!!You will be creating a driver only for this assignment, however, the sorting methods should be entirely outside the main method.You will be creating a driver only for this assignment, however, the sorting methods should be entirely outside the main method.You will be creating a driver only for this assignment, however, the sorting methods should be entirely outside the main method.!!!

Within the driver class, please complete the following tasks:

Rewrite the code for the selection sort, the insertion sort, and the bubble sort so that the integer values within the array are sorted from highest to lowest. Please rename the methods as follows: sectionSort becomes reverseSelection, insertionSort becomes reverseInsertion, and bubbleSort becomes reverseBubble.

Before submitting the code, remove the block of code that prints out the contents of the array after each pass from within each method. Be sure to also delete any variables that are solely related to this task.

Include clear comments throughout the program that explain the well named variables and what functionality the various lines/small sections of code are completing as related to the execution of the program.

Here is the code you will be re writing backwards. copy it into a java program:

package sorting;

public class Sorting {

public static void main(String[] args) { int [] sortArray = {3, 9, 6, 1, 2, 5}; //original array to be sorted int [] sortArray2 = {3, 9, 6, 1, 2, 5}; //duplicate array to be sorted int [] sortArray3 = {3, 9, 6, 1, 2, 5}; //duplicate array to be sorted //print contents of unsorted original array System.out.println("-------Unsorted Array-------"); for (int index=0; index

selectionSort(sortArray); System.out.println("----Sorted Array with selection sort----"); for (int index=0; index 0 && (array2[place - 1] > toSort)) { array2[place] = array2[place - 1]; place--; } array2[place] = toSort; //block of code to print array contents after each pass count++; //counts passes through method //prints out contents of array after each pass System.out.print("After " + count + " pass(es), the array looks like this: "); for(int loop = 0; loop < array2.length; loop++) { System.out.print("\t" + array2[loop]); } System.out.println(); } System.out.println(); //spacing } //Bubble sort public static void bubbleSort(int [] array3) { int temp; //temp value to use in swap int count = 0; System.out.println("-----Running bubble sort-----"); //header for(int index=0; index < array3.length; index++) //work through array from beginning { for(int nextIndex=1; nextIndex < (array3.length-index); nextIndex++) //compare to next { //if the next one is smaller than the one preceding it, swap if(array3[nextIndex-1] > array3[nextIndex]) { //swap elements temp = array3[nextIndex-1]; array3[nextIndex-1] = array3[nextIndex]; array3[nextIndex] = temp; } } //block of code to print array contents after each pass count++; //counts passes through method //prints out contents of array after each pass System.out.print("After " + count + " pass(es), the array looks like this: "); for(int loop = 0; loop < array3.length; loop++) { System.out.print("\t" + array3[loop]); } System.out.println(); } } }

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

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions