-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
36 lines (31 loc) · 1.38 KB
/
ShellSort.java
File metadata and controls
36 lines (31 loc) · 1.38 KB
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
26
27
28
29
30
31
32
33
34
35
36
public class ShellSort {
public static <T extends Comparable<? super T>> void shellSort(T[] array, int start, int end) {
int numEntries = end - start + 1;
int space = numEntries/2;
while (space > 0) {
System.out.println("Starting shellSort, space is: " + space);
for (int i = start; i < start + space; i++) {
incrementalInsertionSort(array, i, end, space);
}
space /= 2;
System.out.println("After for loop, space is: " + space);
}
}
private static <T extends Comparable<? super T>> void incrementalInsertionSort(T[] array, int start, int end, int space) {
int unsorted;
int index;
for (unsorted = start; unsorted <= end; unsorted += space) {
T nextInsert = array[unsorted];
index = unsorted - space;
while ((index >= start) && (nextInsert.compareTo(array[index]) < 0)) {
array[index + space] = array[index];
index -= space;
System.out.println("In incrementalInsertionSort while loop");
SortingDriver.displayCurrent(array);
}
array[index + space] = nextInsert;
System.out.println("In incrementalInsertionSort outside while loop");
SortingDriver.displayCurrent(array);
}
}
}