Created
February 24, 2014 06:02
-
-
Save elliothatch/9182684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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