Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The insertionSort method in the IntInsertionSorter class presented in this chapt

ID: 3652026 • Letter: T

Question

The insertionSort method in the IntInsertionSorter class presented in this chapter sorts an array of int values. Create an ObjectInsertionSorter class that can sort Comparable objects. Demonstrate the class in a program that sorts an array of String objects.


public class IntInsertionSorter

{


public static void insertionSort(int[] array)

{

int unsortedValue;

int scan;


for (int index = 1; index, array.length; index++)

{

unsortedValue = array[index];


scan = index;


while (scan > 0 && array[scan - 1] > unsortedValue)

{

array[scan] = array[scan-1];

scan--;

}

array[scan] = unsortedValue;

}

}

}


From Starting Out With Java From Control Structures through Data Structures, 2nd Edition by Tony Gaddis

Explanation / Answer

//ObjectInsertionSorter class public class ObjectInsertionSorter { public static void insertionSort(Object[] a) { for (int i=0; i = 0; j--) { if (((Comparable)a[j]).compareTo(v)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote