希尔排序
2019-10-21 本文已影响0人
潦倒神仙
public class ShellSort {
public static void sort(int[] arr) {
int j;
int step;
int len = arr.length;
for (step = len / 2; step > 0; step /= 2) {
for (int i = 0; i < step; i++) {
for (j = i + step; j < len; j += step) {
if (arr[j] < arr[j - step]) {
int temp = arr[j];
int k = j - step;
while (k >= 0 && arr[k] > temp) {
arr[k + step] = arr[k];
k -= step;
}
arr[k + step] = temp;
}
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[] { 23, 98, 45, 2, 24, 64, 12, 1 };
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
}