Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi all, for an assignment I'm doing in Java on Netbeans, I've been asked to display both an example of a merge sort and selection

Hi all, for an assignment I'm doing in Java on Netbeans, I've been asked to display both an example of a merge sort and selection sort as well as two differnt charts for each, displaying the length of an array sorted as well as how long it took. Here it is in my instructor's words. In the second section you collect timing data from sorting random arrays of various sizes using both selection sort and merge sort and display the results in a table. See sample output. When running this program to collect final results for submission, close all other applications. For collecting timing data, use rand.nextInt(), without using any specified range.

This confused me. Is there a certain command I can type into the print line to display the time for a certain run? How would I do that many different runs in one run click? Is the answer to input the times manually after running them on my own? If anyone could help I'd appreciate it. Here's a screenshot of the output:

image text in transcribedHere is the code I've written so far:

/* * * */

package asgn03;

import static java.lang.System.arraycopy; import static java.lang.System.out; import java.util.Arrays; import java.util.Random;

/** * * @author anon */ public class Asgn03 { final static Random rand = new Random();

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int[] arr = new int[6]; for (int j = 0; j

// sorts an array of integers (shell function for public use) public static void sort(final int[] arr) { sort(arr, 0, arr.length - 1, 0); } // end public sort

// sort the segment arr[low:high], its length is (high - low + 1) private static void sort(final int[] arr, final int low, final int high, int level) { // Base case: a segment of length

// trace output

// Recursive case: segment length > 1 // Consider arr[low:high] split into arr[low:mid] and arr[mid+1:high] // and sort them recursively where mid = (low + high) / 2 final int mid = (low + high) / 2; sort(arr, low, mid, level + 1); sort(arr, mid + 1, high, level + 1);

// merge arr[low:mid] and arr[mid+1:high] into arr[low:mid] merge(arr, low, high, level); } // end private sort

private static void merge(final int[] arr, final int low, final int high, int level) { // array segment arr[low:high] contains two sorted subsegments arr[low:mid] // and arr[mid+1:high] where mid = (low + high) / 2 final int mid = (low + high) / 2; // temporary array into which the segments arr[low:mid] and arr[mid+1:high] // are merged. The size needed is high - low + 1 final int[] tempArr = new int[high - low + 1];

// index variables set into segments to merge and into the tempArray int index1 = low, index2 = mid + 1, index3 = 0;

while (index1

// number of values left over (not copied into tempArr yet) // from first segment: mid - index1 + 1 // from second segment: high - index2 + 1 // Note that only one segment will have leftovers so // one of the following arraycopy's will not do anything arraycopy(arr, index1, tempArr, index3, mid - index1 + 1); arraycopy(arr, index2, tempArr, index3, high - index2 + 1);

// copy back from tempArr to arr[low:high] arraycopy(tempArr, 0, arr, low, tempArr.length); } // end merge

// sort the segment arr[low:high], its length is (high - low + 1) private static String segmentToString(int[] arr, int low, int high) { if (low > high) return "[]";

// at least one value String str = "[" + arr[low]; for (int index = low + 1; index

private static int getIndexOfMin(int[] arr, int startAt, int lastIndex) { int minIndex = startAt; for (int index = startAt + 1; index

} // end class

Output-Asgn03-Key (run) run: CPS 151 Assignment 3 by kEY Demonstrate Merge Sort riginal array: [711, 98, 666, 458, 354, 330, 798, 914, 597, 432, 631, 95, 828, 41, 701, 142, 83, 376, 925, 446] Sorted array: [41, 83, 95, 98, 142, 330, 354, 376, 432, 446, 458, 597, 631, 666, 701, 711, 798, 828, 914, 925] Demonstrate Selection sort Original array: [782, 219, 266, 934, 214, 680, 825, 571, 601, 791, 343, 127, 796, 561, 833, 605, 72, 518, 103, 673] Sorted array: [72, 103, 127, 214, 219, 266, 343, 518, 561, 571, 601, 605, 673, 680, 782, 791, 796, 825, 833, 934] Merge sort List Size Time (msec) 100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 31 47 63 62 78 109 125 140 156 172 Selection sort List Size Time (msec) 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000 62 219 469 828 1344 1734 2328 3031 3860 4734 Assignment 3 complete BUILD SUCCESSFUL (total time: 19 seconds) Output-Asgn03-Key (run) run: CPS 151 Assignment 3 by kEY Demonstrate Merge Sort riginal array: [711, 98, 666, 458, 354, 330, 798, 914, 597, 432, 631, 95, 828, 41, 701, 142, 83, 376, 925, 446] Sorted array: [41, 83, 95, 98, 142, 330, 354, 376, 432, 446, 458, 597, 631, 666, 701, 711, 798, 828, 914, 925] Demonstrate Selection sort Original array: [782, 219, 266, 934, 214, 680, 825, 571, 601, 791, 343, 127, 796, 561, 833, 605, 72, 518, 103, 673] Sorted array: [72, 103, 127, 214, 219, 266, 343, 518, 561, 571, 601, 605, 673, 680, 782, 791, 796, 825, 833, 934] Merge sort List Size Time (msec) 100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 31 47 63 62 78 109 125 140 156 172 Selection sort List Size Time (msec) 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000 62 219 469 828 1344 1734 2328 3031 3860 4734 Assignment 3 complete BUILD SUCCESSFUL (total time: 19 seconds)

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

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago