Question: 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

2 10 11 12 13 14 15 16 17 SelectionSort.java 1 importjava.util.Arrays; 3 public class SelectionSort { 4567899722222222222237 30 31} 18 19 2021 } } //Don't touch this method! private static void printArray(int[] a)


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: but was: 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

Step by Step Solution

3.34 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

selectionSortint data method implementation selectionSort method implementation public static int se... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!