-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
25 lines (21 loc) · 1005 Bytes
/
InsertionSort.java
File metadata and controls
25 lines (21 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class InsertionSort {
public static <T extends Comparable<? super T>> void insertionSort(T[] array, int start, int end) {
for (int unsorted = start; unsorted <= end; unsorted++) {
T firstUnsorted = array[unsorted];
System.out.println("In insertionSort, firstUnsorted is: " + firstUnsorted);
orderedInsert(firstUnsorted, array, start, unsorted - 1);
}
}
private static <T extends Comparable<? super T>> void orderedInsert(T entry, T[] array, int start, int end) {
int index = end;
while ((index >= start) && (entry.compareTo(array[index]) < 0)) {
array[index + 1] = array[index];
index--;
System.out.println("In orderedInsert while loop");
SortingDriver.displayCurrent(array);
}
array[index + 1] = entry;
System.out.println("In orderedInsert outside while loop");
SortingDriver.displayCurrent(array);
}
}