Question: How can I call the method selectionSort and insertionSort to my main method, so I can use them to sort and an array of integers

How can I call the method selectionSort and insertionSort to my main method, so I can use them to sort and an array of integers that are randomized? The methods I want to call are in a class called ArraySorter, which is a generic class.

Below are the two methods:

// SELECTION SORT /** Sorts the first n objects in an array into ascending order. @param a An array of Comparable objects. @param n An integer > 0. */ public static > void selectionSort(T[] a, int n) { for (int index = 0; index < n - 1; index++) { int indexOfNextSmallest = getIndexOfSmallest(a, index, n - 1); swap(a, index, indexOfNextSmallest); // Assertion: a[0] <= a[1] <= . . . <= a[index] <= all other a[i] } // end for } // end selectionSort // Finds the index of the smallest value in a portion of an array a. // Precondition: a.length > last >= first >= 0. // Returns the index of the smallest value among // a[first], a[first + 1], . . . , a[last]. private static > int getIndexOfSmallest(T[] a, int first, int last) { T min = a[first]; int indexOfMin = first; for (int index = first + 1; index <= last; index++) { if (a[index].compareTo(min) < 0) { min = a[index]; indexOfMin = index; } // end if // Assertion: min is the smallest of a[first] through a[index]. } // end for

return indexOfMin; } // end getIndexOfSmallest

// -------------------------------------------------------------------------------

// INSERTION SORT public static > void insertionSort(T[] a, int n) { insertionSort(a, 0, n - 1); } // end insertionSort

public static > void insertionSort(T[] a, int first, int last) { for (int unsorted = first + 1; unsorted <= last; unsorted++) { // Assertion: a[first] <= a[first + 1] <= ... <= a[unsorted - 1] T firstUnsorted = a[unsorted]; insertInOrder(firstUnsorted, a, first, unsorted - 1); } // end for } // end insertionSort

private static > void insertInOrder(T anEntry, T[] a, int begin, int end) { int index = end; while ((index >= begin) && (anEntry.compareTo(a[index]) < 0)) { a[index + 1] = a[index]; // Make room index--; } // end for // Assertion: a[index + 1] is available a[index + 1] = anEntry; // Insert } // end insertInOrder

// -------------------------------------------------------------------------------

Below is the code in my main method where I am creating the array and where I want to call the two methods to sort the array by selection and insertion.

import java.util.Arrays;

public class client {

public static void main(String[] args) { int[] array = new int [10]; for (int i = 0; i < array.length; i++) array[i] = (int)(Math.random() * 100); System.out.println(Arrays.toString(array));

} }

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!