Skip to content

Instantly share code, notes, and snippets.

@elliothatch
Created February 24, 2014 06:02
Show Gist options
  • Save elliothatch/9182684 to your computer and use it in GitHub Desktop.
Save elliothatch/9182684 to your computer and use it in GitHub Desktop.
/**
* Sorts a subrange of values in an array of Comparable objects using Insertion Sort
* @param data - The array to be sorted
* @param start - Beginning index, inclusive
* @param end - Ending index, inclusive
*/
public static <T extends Comparable<? super T>> void insertionSort(T[] data, int start, int end)
{
for(int i = start; i <= end; i++)
{
T targetVal = data[i];
int currentPosition = i;
while(currentPosition > start && incrementComparisonCount(data[currentPosition -1 ].compareTo(targetVal) > 0))
{
data[currentPosition] = data[currentPosition - 1];
currentPosition--;
}
data[currentPosition] = targetVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment