package log;
public class Test4 {
/**
* java算法---希尔排序(最小增量排序)
*
* @param args
*/
public static void main(String[] args) {
// 需要排序的数组
int arr[] = { 49, 20, 36, 51, 18, 94, 61, 31, 50 };
double d1 = arr.length;
int temp = 0;
// 循环输出该数组内容
System.out.println("排序之前:");
for (int a : arr) {
System.out.print(a + "\t");
}
System.out.println();
while (true) {
d1 = Math.ceil(d1 / 2) % 2 == 0 ? Math.floor(d1 / 2) : Math.ceil(d1 / 2);
int d = (int) d1;
for (int x = 0; x < d; x++) {
for (int i = x + d; i < arr.length; i += d) {
int j = i - d;
temp = arr[i];
for (; j >= 0 && temp < arr[j]; j -= d) {
arr[j + d] = arr[j];
}
arr[j + d] = temp;
}
}
if (d == 1) {
break;
}
}
// 循环输出该数组内容
System.out.println("排序之后:");
for (int a : arr) {
System.out.print(a + "\t");
}
System.out.println();
}
}