Answer:
Step-by-step explanation:
The following code is written in Java it uses an insertion sort algorithm to sort a Comparable array that is passed as an input. The method itseld returns the number of changes that need to be made in order to fully sort the array.
class InsertionSorter {
private static Object IllegalArgumentException;
public static void main(String[] args) throws Throwable {
Comparable[] myarr = {1, 6, 15, 2, 5, 3, 8, 33, 10};
System.out.println(insertionSort(myarr));
}
public static int insertionSort(Comparable[] array) throws Throwable {
try {
int i, j;
Comparable newValue;
int count = 0;
for (i = 1; i < array.length; i++) {
newValue = array[i];
j = i;
while (j > 0 && (array[j - 1].compareTo(newValue)>0)) {
array[j] = array[j - 1];
j--;
count += 1;
}
array[j] = newValue;
}
return count;
} catch (Exception e) {
throw (Throwable) IllegalArgumentException;
}
}
}