Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please expand the main method in the code provided below named JavaTimer.java to meet the requirements. JavaTimer.java: import java.util.Arrays; import java.util.Random; public class JavaTimer {

Please expand the main method in the code provided below named JavaTimer.java to meet the requirements.

image text in transcribed

JavaTimer.java:

import java.util.Arrays; import java.util.Random;

public class JavaTimer {

// Please expand method main() to meet the requirements. // You have the following sorting methods available: // insertionSort(int[] a); // selectionSort(int[] a); // mergeSort(int[] a); // quickSort(int[] a); // The array will be in sorted order after the routines are called! // Be sure to re-randomize the array after each sort. public static void main(String[] args) { // Create and initialize arrays int[] a = {1, 3, 5}, b, c, d; // Check the time to sort array a long startTime = System.nanoTime(); quickSort(a); long endTime = System.nanoTime(); long duration = (endTime - startTime) / 1000l; // Output results System.out.println("Working on an array of length " + a.length + "."); System.out.println("Quick sort: " + duration + "us."); } // Thanks to https://www.javatpoint.com/insertion-sort-in-java public static void insertionSort(int array[]) { int n = array.length; for (int j = 1; j -1) && (array[i] > key)) { array[i + 1] = array[i]; i--; } array[i + 1] = key; } }

// Thanks to // http://www.java2novice.com/java-sorting-algorithms/selection-sort/ public static void selectionSort(int[] arr) { for (int i = 0; i

int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } }

// Thanks to http://stackoverflow.com/questions/13727030/mergesort-in-java public static void mergeSort(int[] A) { if (A.length > 1) { int q = A.length / 2;

// changed range of leftArray from 0-to-q to 0-to-(q-1) int[] leftArray = Arrays.copyOfRange(A, 0, q - 1); // changed range of rightArray from q-to-A.length to // q-to-(A.length-1) int[] rightArray = Arrays.copyOfRange(A, q, A.length - 1);

mergeSort(leftArray); mergeSort(rightArray);

merge(A, leftArray, rightArray); } }

private static void merge(int[] a, int[] l, int[] r) { int totElem = l.length + r.length; // int[] a = new int[totElem]; int i, li, ri; i = li = ri = 0; while (i = l.length) { while (ri = r.length) { while (li

// Thanks to: http://www.algolist.net/Algorithms/Sorting/Quicksort public static void quickSort(int arr[]) { quickSortRecurse(arr, 0, arr.length-1); } private static void quickSortRecurse(int arr[], int left, int right) { int index = partition(arr, left, right); if (left

private static int partition(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2];

while (i pivot) j--; if (i

return i; }

}

A java class is provided below named JavaTimer.java with standard code for four kinds of sort: insertionSort, selectionSort, mergesort, and quickSort. Expand the main() method to do the following: Create the following arrays of random integers

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

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

More Books

Students also viewed these Databases questions

Question

Has the priority order been provided by someone else?

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago