Problem 21.4: Write a generic method selectionSort based on the sort program of
ID: 3632891 • Letter: P
Question
Problem 21.4: Write a generic method selectionSort based on the sort program of Figs. 19.6-19.7. Write a test program that inputs, sorts and outputs and Integer array and a Float array. [Hint: Use <T extends Comparable <T>> in the type-parameter section for method selectionSort, so that you can use method compareTo to compare the objects of the type that T represents.Explanation / Answer
//Needed ArrayIndexOutOfBoundException import java.lang.ArrayIndexOutOfBoundsException; //Create class GenericSelectionSort public class GenericSelectionSort { //Main Method public static void main(String[] args){ try{ // Create an array of Integers Integer [] num = {90,120,30,20,10,60}; // Display Message System.out.println("Integer Elements after sorting "); // Method for sort integer numbers selectionSort(num); //catch the exception }catch (ArrayIndexOutOfBoundsException e1){ } try{ //Create an array of float or double values Double [] num1={12.9,11.8,87.9,10.1,6.9}; System.out.println(" Float Elements after sorting "); //method to sort double values selectionSort(num1); //catch the exception }catch (ArrayIndexOutOfBoundsException e2){ } } // Method to sort the elements in the array public static void selectionSort(T[] list) { for (int i = 1; i = 0 && list[j].compareTo(Element) > 0; j--) { list[j+ 1] = list[j]; } // Insert the current element into list[k+1] list[j + 1] = Element; } //For loop prints the sorted elements for(int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.