Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with my java homework. The code I have so far has been added (the interfaces also) Overview In this assignment you will

Please help me with my java homework. The code I have so far has been added (the interfaces also)

Overview

In this assignment you will be implementing and testing all three sort algorithms: Bubble Sort , Selection Sort , and Insertion Sort .

In additions, you will also be writing a driver to test the search algorithms and you will be measuring the run times of each search.

You will also be using the TestTimes class that you created for Homework 1.

Finally, you will be analysing and comparing the performance of the three sort algorithms based on the type of array that was being sorted and the run times you computed.

Details

  1. TestTimes Class You will copy the TestTimes class that you created in Homework 1 to the project you are using for this assignment.

  2. BubbleSort Class You will write the BubbleSort.java class which will inherit from TestTimes.java and implement the Sort Interface using the Bubble Sort algorithm. The interface may be downloaded from SortInterface.java Please note that your sort method must measure the run time and add it to the TestTimes class by using the addTestTime() method.

  3. SelectionSort Class You will write the SelectionSort.java class which will inherit from TestTimes.java and implement the Sort Interface using the Selection Sort algorithm. The interface may be downloaded from SortInterface.java Please note that your sort method must measure the run time and add it to the TestTimes class by using the addTestTime() method.

  4. InsertionSort Class You will write the InsertionSort.java class which will inherit from TestTimes.java and implement the Sort Interface using the Insertion Sort algorithm. The interface may be downloaded from SortInterface.java Please note that your sort method must measure the run time and add it to the TestTimes class by using the addTestTime() method.

  5. Driver Class You will write the Driver.java class which will implement the Driver Interface . The interface may be downloaded from DriverInterface.java

  6. Output From Driver Main Method Please note that, in addition to implementing the DriverInterface, you are also required to write your own public static main(String[] args)method in Driver.java. Your main() method will have to call the runSort() method to sort each of the following array types ten times for each sort algorithm:

--------------------------------------

InsertionSort

-----------------------------------------

public static void insertionSort(int [] arr){ for(int i=0; i < arr.length; i++){ int loc = i; //arbitrary location int elementToInsert = arr[i]; while( loc > 0 && arr[loc-1] > elementToInsert){ arr[loc] = arr[loc-1]; //shift all necessary elements loc--; } arr[loc] = elementToInsert;//insert the element into the correct spot } }

----------------------------------------------

BubbleSort

----------------------------------------------

public static void bubbleSort(int [] arr){ boolean notSorted = true; while(notSorted){ notSorted = false; //we will set to true if we have to swap for(int i=0; i< arr.length-1; i++){ int j= i+1; if( arr[i] > arr[j] ){ //arr[i].compareTo(arr[j]) > 0 swap(i,j,arr); notSorted = true; //set our flag to true so we re-enter the loop and compare } } } }

---------------------------------------------------------------------

SelectionSort

-------------------------------------------------------------------

public static void selectionSort(int [] arr){ for(int i=0; i< arr.length-1; i++){ int minI = i; for(int j =i+1; j < arr.length; j++){ if(arr[j] < arr[minI]){ minI = j; //new minIndex } } swap(i, minI, arr); } } private static void swap(int i, int j, int[] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }

---------------------------------------------------------------

DriverInterface

------------------------------------------------------------

public interface DriverInterface { public static enum ArrayType {Equal, Random, Increasing, Decreasing, IncreasingAndRandom}; public static enum SortType {BubbleSort, SelectionSort, InsertionSort}; public Integer[] createArray(ArrayType arrayType, int arraySize); public TestTimes runSort(SortType sortType, ArrayType arrayType, int arraySize, int numberOfTimes); }

-------------------------------------------------------------------------------------

SortInterface

------------------------------------------------------------------------------------

public interface SortInterface { /** * * This method is called to sort the given array of Integer objects. At the * completion of this method, the array will be sorted. * * @param arrayToSort This is the array that contains all the Integer objects * that need to be sorted. */ public void sort(Integer[] arrayToSort); public void BubbleSort(int [] arr); public void SelectionSort(int [] arr); public void InsertionSort(int [] arr); }

------------------------------------------------------------

TestTimesInterface

------------------------------------------------------------

public interface TestTimesInterface { // Returns last Test time public long getLastTestTime(); // Return last 10 Test times public long[] getTestTimes(); // Reset all tests public void resetTestTimes(); // Create a new Test time public void addTestTime(long testTime); // Return average of all available Test times public double getAverageTestTime(); }

-------------------------------------------------------------

TestTimes

---------------------------------------------------------------

public class TestTimes { private static long[] testTimes = {0,0,0,0,0,0,0,0,0,0}; // 10 longs private static int counter = 0;

// Override because it's not defined in Interface public long getLastTestTime() { if(counter == 0) return 0; //testTimes[9]; ? else return testTimes[counter-1]; // ok, Ex. if threre's 4 tests // the last is at cell [3]. }

public long[] getTestTimes() { return testTimes; // returns entire array, Note: Array must be init to // 0, if it's to return zeroes for not added tests }

public void resetTestTimes() { for (int i = 0; i < testTimes.length; i++) testTimes[i] = 0; // reset all entries counter = 0; // AND reset counter }

public void addTestTime(long testTime) { // Create one more if( counter <= 9 ) { testTimes[counter] = testTime; counter++; // Increase counter } else { // When adding the 11th test, drop OLDEST test long T[] = new long[10]; // temp array T[9] = testTime; // new test gets put in last cell for (int i = 0; i < testTimes.length - 1; i++) T[i] = testTimes[i+1]; // reset all entries // copy the Temp array into testTimes array for (int i = 0; i < testTimes.length; i++) testTimes[i] = T[i]; } }

public double getAverageTestTime() { int avg = 0; if ( counter != 0 ) { for (int i = 0; i < testTimes.length; i++) avg += testTimes[i]; avg /= counter; } return (avg); } } -------------------------------------------------

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

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

10. What is meant by a feed rate?

Answered: 1 week ago