Write a method, public static int insertSort(ArrayList list), which implements a insertion sort on the ArrayList of Integer objects list. In addition your insertSort method should return an int which represents a statement execution count recording how many times two elements from the ArrayList are compared. For example, if the parameter list prints as [3, 7, 2, 9, 1, 7] before a call to insertSort, it should print as [1, 2, 3, 7, 7, 9] after the method call. This call should return the value 10 since 10 values need to be compared to implement an insertion sort on this array.

Respuesta :

Answer:

Here is the basics of insertionSort performed on an array of integers, this should get you started:

/**

* The insertionSort method performs an insertion sort on an int array. The

* array is sorted in ascending order.

*

* param array The array to sort.

*/

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;

}

}