Question: Write a Java function that creates and returns an array of integers: public static int[] generateNumbers(int howMany) The values should be from 0 to 100000.
Write a Java function that creates and returns an array of integers: public static int[] generateNumbers(int howMany)
The values should be from 0 to 100000.
Use Math.random() to generate random numbers from 0.0 to 1.0.
Write a bubble sort function that sorts an array of integers (lowest to highest): public static SortingStats bubbleSort(int[] data)
Write a selection sort function that sorts an array of integers (lowest to highest): public static SortingStats selectionSort(int[] data)
Report timing on vectors (of random integers) of size 1000 to 10000, stepping by 1000, how long it takes the bubble and selection sort functions.
You'll write a loop to step through these sizes.
Each time in the loop, create one array of random numbers for the bubble sort and a different array (of the same size) of random numbers for the selection sort. These need to be separate arrays, if not, the second sort will use an already sorted array.
provided a SortingStats class for your use in developing this assignment. This class has various methods on it to update the statistics during the sorting. You must use this class and correctly update the statistics in your sorting methods.
In each sorting method, use an object of the SortingStats class to track how many comparisons and swaps occurred during the sort, along with the total time it took to perform the sort. This is returned from the sort methods and used by your code to report the timing, and by the unit tests to help determine correctness.
Use System.currentTimeMillis() to obtain the current time in milliseconds.
public class SortingStats { private int swapCount = 0; private int compareCount = 0; private long timeInMs = 0; public void incrementSwapCount() { swapCount++; } public int getSwapCount() { return swapCount; } public void incrementCompareCount() { compareCount++; } public int getCompareCount() { return compareCount; } public void setTime(long timeInMs) { this.timeInMs = timeInMs; } public long getTimeInMs() { return timeInMs; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
