Question
In chapter 3 in Zybook: Step 1: Run the JAVA programs in sections 3.3 and 3.5 for selection sort and insertion sort. You need to
In chapter 3 in Zybook:
Step 1: Run the JAVA programs in sections 3.3 and 3.5 for selection sort and insertion sort. You need to attach the output results.
Step 2: You need to modify these two programs and add some comments and explanations to them to sort the list elements from high to low value. The index should be IndexHighest instead of IndexSmallest.
Step3 : Run the modified programs, print out the results, and submit them.
In the end, you should have the running results for the original JAVA programs (selection and insertion sort JAVA programs), the two modified programs to sort the values from high to low value for selection and insertion sorts, and the results for the modified programs.
Program 3.3 (Selection Sort):
import java.util.Arrays;
public class SelectionSortDemo { private static int selectionSort(int[] numbers) { // A variable to hold the number of item comparisons int comparisons = 0;
for (int i = 0; i < numbers.length - 1; i++) { // Find index of smallest remaining element int indexSmallest = i; for (int j = i + 1; j < numbers.length; j++) { comparisons++; if (numbers[j] < numbers[indexSmallest]) { indexSmallest = j; } } // Swap numbers[i] and numbers[indexSmallest] int temp = numbers[i]; numbers[i] = numbers[indexSmallest]; numbers[indexSmallest] = temp; } return comparisons; }
public static void main(String[] args) { // Create an array of numbers to sort int[] numbers = { 10, 2, 78, 4, 45, 32, 7, 11 }; // Display the contents of the array System.out.println("UNSORTED: " + Arrays.toString(numbers)); // Call the selectionSort method int comparisons = selectionSort(numbers); // Display the sorted contents of the array System.out.println("SORTED: " + Arrays.toString(numbers)); System.out.println("Total comparisons: " + comparisons); } }
Program 3.5 (Insertion Sort):
import java.util.Arrays;
public class InsertionSortDemo { private static void insertionSort(int[] numbers) { for (int i = 1; i < numbers.length; i++) { int j = i; while (j > 0 && numbers[j] < numbers[j - 1]) { // Swap numbers[j] and numbers [j - 1] int temp = numbers[j]; numbers[j] = numbers[j - 1]; numbers[j - 1] = temp; j--; } } }
public static void main(String[] args) { // Create an array of numbers to sort int[] numbers = { 10, 2, 78, 4, 45, 32, 7, 11 }; // Display the contents of the array System.out.println("UNSORTED: " + Arrays.toString(numbers)); // Call the insertionSort method insertionSort(numbers); // Display the sorted contents of the array System.out.println("SORTED: " + Arrays.toString(numbers)); } }
Step 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