Question
In Java 77. Selection Sort a. Modify the selectionSort function from the Sorting code on Scholar to keep track of the total number of comparisons
In Java 77. Selection Sort
a. Modify the selectionSort function from the Sorting code on Scholar to keep track of the total number of comparisons of elements in the array. Verify that the number of comparisons is correct by testing on small lists where you know what the answer should be by using the (n/2)(n-1) formula. Hand-in the modified method, test-code, and results. (10 points)
b. Create a method that repeatedly ask the user to enter a positive integer. For each positive integer entered, the method should call the makeRandomArray function from the Sorting code on Scholar to generate an array of that size containing random numbers (in any range you want), sort the array using the modified selectionSort from part (a), and display the number of comparisons taken. Tell the user what to enter to stop the program. Hand-in just the method; no testing needs to be shown. (10 points)
c. Run the method in (c) and enter 10, 20, 50, 500, 5000, 100,000 as positive integers for the array sizes. Fill in the table below, and compare the actual number of comparisons with the expected value of (n/2)(n-1). Hand-in the completed table. (10 points) Notice how each time n increases by a factor of x, the number of comparisons increases by approximately x2. At 100,000 the algorithm will take a few seconds to run, and it will get much slower if n is increased further (Try 200,000 and larger numbers to see). n Actual Comparisons (n/2)(n-1) 10 20 50 500 5000 100,000 NOTE: If the actual comparisons for n = 100,000 doesnt match the formula, something is wrong. Investigate. If this causes you to make any changes to your code in (a) or (b), make sure to replace what you are handing in with the updated version.
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