借鉴:借鉴跳转,上面都有详细的描述以及动图的实现。
希尔排序
/**
* 简单排序-希尔排序
* 从小到大排序
*/
public class ShellSort {
private static int[] shellSort(int[] arr) {
int length = arr.length;
int temp, len = length / 2;
while (len > 0) {
for (int i = len; i < length; i++) {
temp = arr[i];
int preindex = i - len;
while ( preindex >= 0 && arr[preindex] > temp ) {
arr[preindex + len] = arr[preindex];
arr[preindex] = temp;
preindex -= len;
}
}
len = len/2;
}
return arr;
}
public static void main(String[] args) {
int[] arr = new int[]{1,2,6,5,3,2,0,10,12,1,12};
shellSort(arr);
for (int it:
arr) {
System.out.println(it);
}
System.out.println();
}
}