Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Above is my instructions, below is my code. Can someone just double check me and ensure that if I 100000 in the coding to Adjust

image text in transcribed

Above is my instructions, below is my code. Can someone just double check me and ensure that if I "100000" in the coding to "Adjust your array sizes to hold only 1,000 elements and run the 9 tests again" that it will in fact meet what I the instructions are asking. I'd like the outputs to automatically line up, but I think the way I coded it, it wont ever truly line up. Is it an easy fix?

import java.util.Arrays;

import java.util.Random;

public class SelectionSort

{

static Random r=new Random();

public static void main(String args[])

{

System.out.println("sort\t\t\t best case\t\t average case\t\t worst case");

SelectionSortTest();

InsertionSortTest();

MergeSortTest();

}

public static void SelectionSortTest()

{

long starttime;

long endtime;

starttime=System.nanoTime();

selectionSort(getSequentialArray());

endtime=System.nanoTime();

long best=endtime-starttime;

starttime=System.nanoTime();

selectionSort(getRandomArray());

endtime=System.nanoTime();

long average=endtime-starttime;

starttime=System.nanoTime();

selectionSort(getDescendingArray());

endtime=System.nanoTime();

long worst=endtime-starttime;

System.out.println(String.format("selection sort\t\t%d\t\t%d\t\t%d",best,average,worst));

}

public static void InsertionSortTest()

{

long starttime;

long endtime;

starttime=System.nanoTime();

insertionSort(getSequentialArray());

endtime=System.nanoTime();

long best=endtime-starttime;

starttime=System.nanoTime();

insertionSort(getRandomArray());

endtime=System.nanoTime();

long average=endtime-starttime;

starttime=System.nanoTime();

insertionSort(getDescendingArray());

endtime=System.nanoTime();

long worst=endtime-starttime;

System.out.println(String.format("Insertion sort\t\t%d\t\t\t%d\t\t%d",best,average,worst));

}

public static void MergeSortTest()

{

long starttime;

long endtime;

starttime=System.nanoTime();

MergeSort(getSequentialArray());

endtime=System.nanoTime();

long best=endtime-starttime;

starttime=System.nanoTime();

MergeSort(getRandomArray());

endtime=System.nanoTime();

long average=endtime-starttime;

starttime=System.nanoTime();

MergeSort(getDescendingArray());

endtime=System.nanoTime();

long worst=endtime-starttime;

System.out.println(String.format("Merge sort\t\t%d\t\t\t%d\t\t%d",best,average,worst));

}

public static int[] getSequentialArray()

{

int[] a=new int[100000];

for(int i=0;i

{

a[i]=i+1;

}

return a;

}

public static int[] getRandomArray()

{

int[] a=new int[100000];

for(int i=0;i

{

a[i]=r.nextInt(100000 + 1 - 1) + 1;

}

return a;

}

public static int[] getDescendingArray()

{

int[] a=new int[100000];

int j=1000;

for(int i=0;i

{

a[i]=j;

j--;

}

return a;

}

public static void insertionSort(int[] data)

{

// loop over data.length - 1 elements

for (int next = 1; next

{

int insert = data[next]; // value to insert

int moveItem = next; // location to place element

// search for place to put current element

while (moveItem > 0 && data[moveItem - 1] > insert)

{

// shift element right one slot

data[moveItem] = data[moveItem - 1];

moveItem--;

}

data[moveItem] = insert; // place inserted element

}

}

public static void selectionSort(int[] data)

{

// loop over data.length - 1 elements

for (int i = 0; i

{

int smallest = i; // first index of remaining array

// loop to find index of smallest element

for (int index = i + 1; index

if (data[index]

smallest = index;

swap(data, i, smallest); // swap smallest element into position

}

} // end method selectionSort

// helper method to swap values in two elements

private static void swap(int[] data, int first, int second)

{

int temporary = data[first]; // store first in temporary

data[first] = data[second]; // replace first with second

data[second] = temporary; // put temporary in second

}

private static int[] numbers;

private static int[] helper;

private static int number;

public static void MergeSort(int[] values)

{

numbers = values;

number = values.length;

helper = new int[number];

mergesort(0, number - 1);

}

private static void mergesort(int low, int high) {

// check if low is smaller than high, if not then the array is sorted

if (low

{

// Get the index of the element which is in the middle

int middle = low + (high - low) / 2;

// Sort the left side of the array

mergesort(low, middle);

// Sort the right side of the array

mergesort(middle + 1, high);

// Combine them both

merge(low, middle, high);

}

}

private static void merge(int low, int middle, int high)

{

// Copy both parts into the helper array

for (int i = low; i

{

helper[i] = numbers[i];

}

int i = low;

int j = middle + 1;

int k = low;

// Copy the smallest values from either the left or the right side back

// to the original array

while (i

{

if (helper[i]

{

numbers[k] = helper[i];

i++;

}

else

{

numbers[k] = helper[j];

j++;

}

k++;

}

// Copy the rest of the left side of the array into the target array

while (i

{

numbers[k] = helper[i];

k++;

i++;

}

}

1. Create an Evaluator class that will evaluate the sorting algorithms you learned about in this chapter. a. Create 1 method for each of the sorting algorithms below. Each method must accept 1 int[] as a parameter. Ensure that the name of your method includes your last name (e.g., John Doe might use a method name like this: selectionSort Doe). i. Selection sort ii. Insertion sort iii. Merge sort b. Implement the code for each of the sort methods above by referring to Figures 19.4 (pp. 814815), 19.5 (pp. 817-819), and 19.6 (pp. 820-822) in the textbook. C. Exclude any portions of the textbook code that print anything to the output window. The goal here is to evaluate the efficiency of the sort algorithms, not how quickly they can print things to the console. d. Add 3 further methods to the Evaluator class that perform the following tasks: i. Returns an array with 100,000 int values in sequential order, starting with 1 and ending with 100,000. ii. Returns an array with 100,000 random int values. Returns an array with 100,000 int values in descending sequential order, starting with 100,000 and ending with 1. 2. In the main method: a. Use the Evaluator class to evaluate each sorting algorithm with each of the 3 arrays (best, average, and worst case) for a total of 9 distinct tests. i. Store the result of System.nanoTime () before and after each call to the sorting method, and calculate the time in nano-seconds it takes to complete each test (i.e., subtract the time taken before the test from the time taken after the test). ii. Generate new arrays prior to each test, but do not include the generation of the arrays in the evaluation of sort time. 6. Output a table showing the best, average, and worst case times for each of the sorting algorithms. 3. Take a screenshot of your output table and paste it into the Word document you created for the first exercise, but place the screen shot under a header for "Exercise 2." Following the screenshot in the same document, write a brief paragraph (100-200 words) on your findings that comments on whether your observed values are consistent with the Big O notation for each sorting algorithm that the textbook provides (see Figure 19.7 on p. 825). If your results differ substantially from the book, discuss why you believe your results jii

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago