package com.ifreewolf.test.sort;
import java.util.logging.SimpleFormatter;
/**
* @author fgs
* @since 2020/10/11 10:10
*/
public class ShellSort {
public static int SIZE = 10;
public static void shellSort(int[] a) {
int i, j, h;
int r, temp;
int x = 0;
for (r = a.length / 2; r >= 1; r /= 2) {
for (i = r; i < a.length; ++i) {
temp = a[i];
j = i -r;
while (j >= 0 && temp < a[j]){
a[j+r] = a[j];
j -= r;
}
a[j+r] = temp;
}
x++;
System.out.println("第" + x + "步排序结果:");
for (h = 0; h < a.length; h++) {
System.out.print(" " + a[h]);
}
System.out.println();
}
}
public static void main(String[] args) {
int[] shuzu = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
shuzu[i] = (int)(100 + Math.random()*(101));
}
System.out.println("排序前的数组为:");
for (int i = 0; i < SIZE; i++) {
System.out.print(shuzu[i] + " ");
}
System.out.println();
shellSort(shuzu);
for (int i = 0; i < SIZE; i++) {
System.out.print(shuzu[i] + " ");
}
System.out.println();
}
}