2 10 11 12 13 14 15 16 17 SelectionSort.java 1 import java.util.Arrays; 3 public class...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
2 10 11 12 13 14 15 16 17 SelectionSort.java 1 import java.util.Arrays; 3 public class SelectionSort { 4567899722222222222237 30 31} 18 19 20 21 } } //Don't touch this method! private static void printArray(int[] a) { System.out.println(Arrays.toString(a)); //Complete this method. public static int[] selectionSort (int[] data) { //Implement a Selection Sort on data here! return data; //You can mess around in the main method //as you wish. As long as it compiles, //it won't affect the testing. public static void main(String[] args) { int[] testData = [45, 93, 33, 55}; System.out.println("Sorting."); testData = selectionSort (testData); System.out.println("After sorting the array is: "); printArray(testData); In this exercise the goal is to implement the Selection Sort algorithm on a small array, in a similar manner to the Bubble Sort required exercise. If you are comfortable with loops and arrays, you can solve this by implementing the full algorithms. If you are not, you can "unroll" the loop and implement each step by hand by hard coding each pass through the array as a sequence of explicit checks and potential swaps. The skeleton contains two methods: 1. A main method which will allow you to do your own testing, and give a working example. 2. A selectionSort method where you'll do the actual implementation of the sorting algorithm. The selectionSort method takes in an int[] called data. This is the array that you are to sort. You are guaranteed for this exercise that data will have length 4. At the end of the method you should return data; . This line has already been added, so you can just leave it as is. For the tests to ensure you are correctly implementing a Selection Sort (and not some other sort, or using a library), you must print out the array using printArray(data) every time you swap elements in the array. You should not print it out any other time in the selectionSort method. For consistency in which version of Selection Sort is implemented: 1. Stop the sort once the unsorted area is only one element. 2. Don't swap if the minimum element is the first element in the unsorted area (so no print out in this case). Feedback < Lessons = Slides Prev Next Week 6 Lab: Where you write some useful array operations Description ✓ Feedback F + Selection Sort (Required Exercise) SelectionSort.java 1 import java.util.Arrays; 2 3 public class SelectionSort { 4 1 2 3 <> Min Method ✓ <> Sum Method 5 //Don't touch this method! Not yet 6 7 private static void printArray(int[] a) { System.out.println(Arrays.toString(a)); 8 } <> Counting Occurrences TESTCASES 2/4 passed 9 <> Bubble Sort (Required Exercise) 10 11 testExampleData 12 Selection Sort (Required <> 13 Exercise) testSorted ✓ 14 15 //Complete this method. public static int[] selectionSort (int[] data) { for (int i=0; i< data.length-1; i++) { for (int j= i +1; j< data.length; j++) { if(data[i]>data[j]) { int temp = data[i]; testReverseSorted ☑ 16 data[i] = data[j]; 17 Failed: At least one swap was incorrect. ==> expected: <[21, 63, 33, 76] 18 data[j] = temp; printArray(data); 19 } [21, 33, 63, 76]> but was: <[63, 76, 33, 21] 20 [33, 76, 63, 21] 21 } [21, 76, 63, 33] 22 23 [21, 63, 76, 33] [21, 33, 76, 63] [21, 33, 63, 76]> Show stacktrace > testOtherData /home/SelectionSort.java 12:6 Spaces: 4 (Auto) Console Terminal Sorting. [33, 93, 45, 55] [33, 45, 93, 55] [33, 45, 55, 93] After sorting the array is: [33, 45, 55, 93] ✓ Program exited with code 0 ✰ Challenge Submissions *** > * All changes saved ‣ Run ✓ Mark 2 10 11 12 13 14 15 16 17 SelectionSort.java 1 import java.util.Arrays; 3 public class SelectionSort { 4567899722222222222237 30 31} 18 19 20 21 } } //Don't touch this method! private static void printArray(int[] a) { System.out.println(Arrays.toString(a)); //Complete this method. public static int[] selectionSort (int[] data) { //Implement a Selection Sort on data here! return data; //You can mess around in the main method //as you wish. As long as it compiles, //it won't affect the testing. public static void main(String[] args) { int[] testData = [45, 93, 33, 55}; System.out.println("Sorting."); testData = selectionSort (testData); System.out.println("After sorting the array is: "); printArray(testData); In this exercise the goal is to implement the Selection Sort algorithm on a small array, in a similar manner to the Bubble Sort required exercise. If you are comfortable with loops and arrays, you can solve this by implementing the full algorithms. If you are not, you can "unroll" the loop and implement each step by hand by hard coding each pass through the array as a sequence of explicit checks and potential swaps. The skeleton contains two methods: 1. A main method which will allow you to do your own testing, and give a working example. 2. A selectionSort method where you'll do the actual implementation of the sorting algorithm. The selectionSort method takes in an int[] called data. This is the array that you are to sort. You are guaranteed for this exercise that data will have length 4. At the end of the method you should return data; . This line has already been added, so you can just leave it as is. For the tests to ensure you are correctly implementing a Selection Sort (and not some other sort, or using a library), you must print out the array using printArray(data) every time you swap elements in the array. You should not print it out any other time in the selectionSort method. For consistency in which version of Selection Sort is implemented: 1. Stop the sort once the unsorted area is only one element. 2. Don't swap if the minimum element is the first element in the unsorted area (so no print out in this case). Feedback < Lessons = Slides Prev Next Week 6 Lab: Where you write some useful array operations Description ✓ Feedback F + Selection Sort (Required Exercise) SelectionSort.java 1 import java.util.Arrays; 2 3 public class SelectionSort { 4 1 2 3 <> Min Method ✓ <> Sum Method 5 //Don't touch this method! Not yet 6 7 private static void printArray(int[] a) { System.out.println(Arrays.toString(a)); 8 } <> Counting Occurrences TESTCASES 2/4 passed 9 <> Bubble Sort (Required Exercise) 10 11 testExampleData 12 Selection Sort (Required <> 13 Exercise) testSorted ✓ 14 15 //Complete this method. public static int[] selectionSort (int[] data) { for (int i=0; i< data.length-1; i++) { for (int j= i +1; j< data.length; j++) { if(data[i]>data[j]) { int temp = data[i]; testReverseSorted ☑ 16 data[i] = data[j]; 17 Failed: At least one swap was incorrect. ==> expected: <[21, 63, 33, 76] 18 data[j] = temp; printArray(data); 19 } [21, 33, 63, 76]> but was: <[63, 76, 33, 21] 20 [33, 76, 63, 21] 21 } [21, 76, 63, 33] 22 23 [21, 63, 76, 33] [21, 33, 76, 63] [21, 33, 63, 76]> Show stacktrace > testOtherData /home/SelectionSort.java 12:6 Spaces: 4 (Auto) Console Terminal Sorting. [33, 93, 45, 55] [33, 45, 93, 55] [33, 45, 55, 93] After sorting the array is: [33, 45, 55, 93] ✓ Program exited with code 0 ✰ Challenge Submissions *** > * All changes saved ‣ Run ✓ Mark
Expert Answer:
Answer rating: 100% (QA)
selectionSortint data method implementation selectionSort method implementation public static int se... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
The following additional information is available about flight 482: Members of the flight crew are paid fixed annual salaries, whereas the flight assistants are paid based on the number of round...
-
If the focal length of a lens is 3 centimeters and the image distance is 5 centimeters from the lens, what is the distance from the object to the lens?
-
Three transactions for Sunland Gifts are presented here. On October 2, Sunland Gifts purchased $35,500 of merchandise from Sea Toys, terms 1/10, n/30. On October 6, Sunland Gifts returned $2,800 of...
-
Mr. Silkwallah established the Fashion Clothing Company (FCC) to market designer clothes. The business was to get designer clothes produced by tailors, exclusively for FCC. FCC provides the following...
-
Use Euler's method to estimate the value of pit) from the selection differential equation (Equation 5.1.4) for the given parameter values. Compare with the exact answer using the equation for the...
-
The efficiency of a refrigerator increases by 3 percent for each C rise in the minimum temperature in the device. What is the increase in the efficiency for each (a) K, (b) F, and (c) R rise in...
-
Which of the following is NOT an implication of Section 302 of the Sarbanes-Oxley Act? a. Auditors must determine, whether changes in internal control has, or is likely to, materially affect internal...
-
The following CVP income statements are available for Old Company and New Company. Instructions (a) Compute the break-even point in dollars and the margin of safety ratio for each company. (b)...
-
Where is the vertical asymptote of g(x) = -3 log ((x-2)) - 5? a) x=1 b) x=0 c) x=-2 d) x=2
-
Lang and Thomas, a local CPA firm, has been asked to bid on a contract to perform audits for three counties in its home state. Should the firm be awarded the contract, it must hire one new staff...
-
JIim Smith, age 22, has the W2 listed at the bottom of this file. He lives in Florida. He has no dependents. If no information is provided for an item needed on the return, assume that it is zero,...
-
In salary problems susceptible to linear programming, which of the following conditions or determinations should apply? a. The purpose ofthe model. b. Restraints on the decision maker's resources. c....
-
What are the ways that social media sites are affecting courtrooms?
-
How can the statements of purpose and scope contribute to understanding the audit report?
-
The most appropriate technique for developing a multi process production schedule is: a. Monte Carlo method. b. Regressing analysis. c. Queuing theory. d. Linear programming.
-
How can you formally obtain discovery of information about the adverse party posted on a social media site?
-
Question 1 (5 marks) An economy is described by the following equation Cd-14400+0.5(Y-T)-40000r, IP-8000-20000r, G-7800, NX-1800, T-8000 a) Find the numerical equation relating planned aggregate...
-
Government is advised to tax goods whose demand curves are inelastic if the goal is to raise tax revenues. If the goal is to discourage consumption, then it ought to tax goods whose demand curves are...
-
Describe the alternative workplace approach.
-
Given the sales data from Problem 23, generate forecasts for months 724 using a six-period and a three-period moving average. Use MAD to compare the forecasts. Which forecast is more stable? Which is...
-
Define the terms total quality management, just-in-time, and reengineering. What do these terms have in common?
-
What is Modigliani and Millers 1977 theory based on and what conclusions do they draw?
-
True or false? The more debt we incur, the higher the interest rate we are charged. Our shareholders also require a higher return. Additionally, if we want a low cost of capital, we have to have a...
-
True or false? By reducing financial leverage, we reduce the cost of debt and the cost of equity, and accordingly, the weighted average cost of capital? Why?
Study smarter with the SolutionInn App