Hello, I have been having quite a bit of trouble with this one. Can someone please tell me how many swaps it would require to sort the following array? {2, 4, 6, 3, 7, 1} I tried putting my counter in a couple of places and neither one of them add up to what I get by simply counting the swaps manually. My manual count is two.
Thank you,
Hank.
/**
The IntInsertionSorter class provides a public static
method for performing an insertion sort on an int array.
*/
public class IntInsertionSorter2
{
private static int count = 0;
/**
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 insertionSort2(int[] array)
{
int unsortedValue; // The first unsorted value
int scan; // Used to scan the array
// The outer loop steps the index variable through
// each subscript in the array, starting at 1. The portion of
// the array containing element 0 by itself is already sorted.
for (int index = 1; index < array.length; index++)
{
// The first element outside the sorted portion is
// array[index]. Store the value of this element
// in unsortedValue.
unsortedValue = array[index];
// Start scan at the subscript of the first element
// outside the sorted part.
scan = index;
// Move the first element in the still unsorted part
// into its proper position within the sorted part.
while (scan > 0 && array[scan-1] > unsortedValue)
{
array[scan] = array[scan - 1];
scan--;
count ++;
}
// Insert the unsorted value in its proper position
// within the sorted subset.
array[scan] = unsortedValue;
}
//System.out.println(swap);
System.out.println("count" + count);
}
}
Emphasized Text Here