Question
PLEASE MODIFY THE PRGORAM BELOW AS IT'S REQURED IN THE ASSIGNMENT WITH EXPLAINATION FOR THE STEPS ! ASSIGNMENT: Task 1: Add a static method that
PLEASE MODIFY THE PRGORAM BELOW AS IT'S REQURED IN THE ASSIGNMENT WITH EXPLAINATION FOR THE STEPS !
ASSIGNMENT:
Task 1: Add a static method that creates a new array of integers and fills the array with random numbers. The method is given the size of the array as a parameter and returns the new array. Do not use a Java library method to do this.
Task 2: Add a static method that copies an array. The method is given an array as a parameter and returns a new array containing the same integers in the same order as the given array. Do not use a Java library method to do this.
Task 3: Write a main method that tests Selection Sort and Bubble Sort. The method must do the following:
create an array of 30 random integers
create a second array that is a copy of the first array
print the first array (using Arrays.toString() provide the array as a parameter)
call Selection Sort to sort the first array
print the sorted array to make sure the numbers are in order
call Bubble Sort to sort the second array
print the second sorted array to make sure the numbers are in order
Run the program a few times to verify that the sorting methods are correct.
Task 4: Modify the main method so that it computes the amount of time each sorting algorithm takes to sort an array of integers. You will do this by creating a Date object (import java.util.Date) immediately before you call Selection Sort and another Date object immediately after you call Selection Sort. Each Date object records the time at which it is created. You can get the time (in milliseconds) from a Date object by calling the objects getTime() method. Use the times to compute how long Selection Sort took and print the result.
Do the same for Bubble Sort.
Before you test your program, you must increase the array size. An array of 30 integers will be sorted so fast, the time will probably be reported as 0. Instead, use an array size of 1000.
Task 5: You will now conduct more tests and record the resulting times. Use the following array sizes and record the times for Selection Sort and Bubble Sort in each case.
Test 1: 1000
Test 2: 10,000
Test 3: 50,000
Test 4: 75,000
Test 5: 100,000
Test 6: 150,000
Test 7: 200,000
Task 6: Modify the Selection Sort and Bubble Sort methods so that each one counts the number of swaps performed. Each method must print the final count when done.
Repeat the above Tests 1-5 and record the resulting number of swaps for Selection Sort and Bubble Sort.
Task 7: Finally, you will implement the Heap Sort algorithm. In this method, create a new PriorityQueue of Integer objects (import java.util.PriorityQueue). Write a loop to add all of the integers from the given array to the PriorityQueue. Then, write another loop to remove all of the integers from the PriorityQueue and put them back in the array, left-to-right. Since the integers are automatically removed from the PriorityQueue in ascending order, you will end up with a sorted array.
Run a test of Heap Sort on an array of 30 integers. Print the random and sorted arrays to verify that Heap Sort actually sorts the array. Repeat the above Tests 1-8 just for Heap Sort and record the times.
_______________________________________________________________________________________________
THE CODE:
public class LoopSort { /** * Sorts the integers in the given array so that they are in ascending order. * @param a array to sort */ public static void selectionSort(int[] a) { for(int i=0; ia[i+1]) { swap(a, i, i+1); swapped = true; } } } } /** * Swaps the two integers in the given array at the given indexes. * @param a array * @param x index of first number to swap * @param y index of second number to swap */ public static void swap(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; } /** * Returns the index of the smallest integer in the given array * starting at the given index. * @param a array to examine * @param start index to start examining array * @return smallest int in array between start and length-1 */ public static int findMin(int[] a, int start) { int min = start; for(int i=start+1; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started