Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me with my question as soon as possible. Please use JAVA programming. You will implement the well-known selection sort algorithm. This algorithm works
Please help me with my question as soon as possible.
Please use JAVA programming.
You will implement the well-known selection sort algorithm. This algorithm works by re- arranging the values through an alternation of searching steps and swapping steps. First, the algorithm locates the minimum value in the array, and then swaps it to the beginning. It then continually finds the next-smallest value and places it into the next spot of the array, until all values have been processed. 1. You'll need to be able to print arrays, so write a short printArray method which takes an array of integers as as a parameter, and, well, prints them. You can pick the format. You can also reuse your code from the last lab for this part if you prefer. 2. Write a method named randArray which takes an integer as input and returns an array of the specified number of random integers from the range 0-100. This method will provide you with some randomized test data for validating your sorting algorithm. Once you finish this method, write a short main method that simply prints a random array of 10 integers. As an example, randArray(5) might return the array {75, 13, 4, 89, 33}. Hint: use an instance of the built-in java.util. Random class and store it in a static variable. Now it's time to build the sorting algorithm! We'll do it step-by-step. 1. Recall that arrays are reference types. Prove that arrays are passed by reference by writing a method named swap which takes an integer array and two integer indices to swap. As an example, if A is the array {75, 13, 4, 89, 33}, then after calling swap(A,0,2) the array should have the value: {4, 13, 75, 89, 33}. (Notice that the element in the oth position and the 2nd position have been swapped. 2. Write a method named swapMin(int[] a, int start) which swaps the minimum of the values a [start], a[start + 1], ..., a[a.length-1] with the value at position a[start]. Continuing with examples, if A has value {4, 13, 75, 89, 33}, then after calling swapMin(A, 0) the array will be unchanged. (Note that A[0] is already the minimum value.). After calling swapMin(A, 2) the value of the array should be {4, 13, 33, 89, 33}. Hint: your swapMin method should use your swap method. 3. Now for the main event! Write a method named sortArray which implements the selection sort algorithm. This method should be pretty short - you just have to call swapMin once for each position of the array. 4. Once you've got your sortArray method working, modify your main method to print the result of sorting your randomly generated array. Then all that's left is to clean up your code and demo to your TA. Congrats, you've implemented a sorting algorithmStep 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