import java.util.Arrays;
public class ShellSort {
public static void shellSort(int[] a) {
int len = a.length;
int i,j;
int h;
int temp;
for (h = len/2; h > 0; h = h/2) {
for (i = h; i < len; i++) {
temp = a[i];
for (j = i-h; j >= 0; j -= h) {
if (temp < a[j]) {
a[j+h] = a[j];
} else {
break;
}
}
a[j+h] = temp;
}
}
}
public static void main(String[] args) {
int[] a = {5, 4, 9, 8, 6, 0, 1, 3, 2};
shellSort(a);
System.out.println(Arrays.toString(a));
}
}
【Java】希尔排序
最新推荐文章于 2024-06-19 15:09:37 发布