Question: Exercise 8.4.5: Explore Selection Sort In this exercise, we will be looking at our example code for Selection Sort. However, while we are sorting we

Exercise 8.4.5: Explore Selection Sort

In this exercise, we will be looking at our example code for Selection Sort. However, while we are sorting we will also count the number of swaps taking place, then print them out once the array has been sorted.

Add a print statement at the end of the selectionSort method that prints out the number of swaps that took place during the sort.

You should not modify the run() method.

Hint: where are items compared? Try writing out the steps in the algorithm on paper to help.

Exercise 8.4.5: Explore Selection Sort In this exercise, we will be looking

Here is the given code to modify:

import java.util.Arrays;

public class SelectionSort extends ConsoleProgram { public void run() { int[] array1 = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int[] array2 = {5, 6, 4, 8, 9, 7, 3, 1, 2}; System.out.print("First array: "); System.out.println(Arrays.toString(array1)); System.out.print("Second array: "); System.out.println(Arrays.toString(array2)); System.out.println();

// sort first array selectionSort(array1); // sort second array selectionSort(array2);

System.out.print("First array sorted: "); System.out.println(Arrays.toString(array1)); System.out.print("Second array sorted: "); System.out.println(Arrays.toString(array2)); } /* * Selection sort takes in an array of integers and * returns a sorted array of the same integers. */ public static int[] selectionSort(int[] arr) { int currentMinIndex; for (int i = 0; i

at our example code for Selection Sort. However, while we are sorting

Here is the output for running the code without any modifications:

First array: [9, 8, 7, 6, 5, 4, 3, 2, 1] Second array: [5, 6, 4, 8, 9, 7, 3, 1, 2] First array sorted: [1, 2, 3, 4, 5, 6, 7, 8, 9] Second array sorted: [1, 2, 3, 4, 5, 6, 7, 8, 9]

we will also count the number of swaps taking place, then print

Here is what the codehs website says when I test cases without modifying the code at all:

them out once the array has been sorted. Add a print statement

Here is the Console Program just in case:

at the end of the selectionSort method that prints out the number

Please help with this, I have been stuck on it for a while now. Let me know if any more information is needed and I will give it to you. Thank you in advance!

7 points Status: Not Submitted In this exercise, we will be looking at our example code for Selection Sort. However, while we are sorting we will also count the number of swaps taking place, then print them out once the array has been sorted. Add a print statement at the end of the selectionsort method that prints out the number of swaps that took place during the sort. You should not modify the run() method. Hint: where are items compared? Try writing out the steps in the algorithm on paper to help 8.4.5: Explore Selection Sort 2 1 import java.util.Arrays; public class Selectionsort extends ConsoleProgram !! public void run() int[] array1 = {9, 8, 7, 6, 5, 4, 3, 2, 1); int[] array2 = {5, 6, 4, 8, 9, 7, 3, 1, 2}; System.out.print("First array: "); System.out.println(Arrays.toString(array)); System.out.print("Second array: "); System.out.println(Arrays.toString(array2)); System.out.println(); // sort first array selectionsort(array1); 1/ sort second array selectionsort(array2); System.out.print("First array sorted: "); System.out.println(Arrays.toString(array)); System.out.print("second array sorted: "); System.out.println(Arrays.tostring(array2)); * Selection sort takes in an array of integers and returns a sorted array of the same integers, public static int[] selectionsort(int[] arr) int currentMinIndex; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!