/**
* 希尔排序
*
* @param array
* @param <C>
*/
public static <C extends Comparable> void sort(C[] array) {
if (null == array || 1 >= array.length) {
return;
}
// 奇数补1,偶数不补
int skip = array.length / 2 + (array.length % 2 == 1 ? 1 : 0);
while (skip >= 1) {
for (int i = 0; i + skip < array.length; i++) {
cas(array, i, i + skip);
}
skip /= 2;
}
}
/**
* 比较/交换值
*
* @param array
* @param a
* @param b
* @param <C>
*/
@SuppressWarnings("unchecked")
private static <C extends Comparable> void cas(C[] array, int a, int b) {
if (array[a].compareTo(array[b]) > 0) {
C t = array[a];
array[a] = array[b];
array[b] = t;
}
}