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
-
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...
-
Gamers' World buys 1,000 computer game CDs from a distributor that is discontinuing those games. The purchase price for the lot is $7,500. Gamers' World will group the CDs into three price categories...
-
For each of the following pairs of polymers, plot and label schematic stressstrain curves on the same graph [i.e., make separate plots for parts (a), (b), and (c)]. (a) Isotactic and linear...
-
On May 8, 1984, Hannah Motsi graduated from Louisiana State University. She set to work opening a coffee shop in Baton Rouge called HannaH and found a perfect location in a new development. Using a...
-
Three cubes, of sides l0 = 2l0 3l0, are placed next to one another (in contact) with their centers along straight line and the l = 2l0 cube in the center (Fig 7-39). What is the position, along this...
-
Find the derivative of the function f(x) = {3x^2 + 2x - 5}/{x^2 +1}
-
Net present value method The following data are accumulated by Waiola Company in evaluating the purchase of $172,300 of equipment, having a 4-year useful life: Net Income Net Cash Flow Year 1 $40,000...
-
What is the value of the z variable after this Java code executes? int x = 77; int y=88; int z = 99; x = y; z = y;
-
Your investment of $9,000 that you started six years ago earned 7.3% compounded quarterly for the first 3 years, followed by 8.2% compounded monthly after that. How much interest has your investment...
-
A wet lease is similar to chartering an aircraft. What are the two primary differences between a wet lease and a charter? Explain.
-
The Viral Clean ("Clean") owners are aware of the importance of c ontracts to both buy and sell goods and services. They are reviewing the Uniform Commercial Code (UCC), Article 2, that covers...
-
Compute the difference in profitability between Spotify domestic and international operations. Does the difference compensate investors for the different levels of risk?
-
Select any MNC for international Finannce and Show the work in excel sheet see the picture below on what information will go into the excel sheet. The analysis should base on a real MNC, not a...
-
Solve the relation Exz:Solve therelation ne %3D
-
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?
-
Use SecureRandom method ints to generate a stream of 50 random numbers in the range 1 to 999, then filter the resulting stream elements to select only the odd numbers and display the results in...
-
(a) What percentage of those not completing high school were females? (b) What percentage of those not completing high school were males? (c) What percentage of those completing high school were...
-
What percentage of the 9,449 survey respondents live in the Eastern part of the county?
Study smarter with the SolutionInn App