classMain {
publicstaticvoidmain(String[] args) {
//希尔排序int[] arr = {34, 53, 77, 22, 35, 97, 4, 15};
int h = 1;
while ( h < arr.length/3){
h = h * 3 + 1;
}
for ( int gap = h; gap > 0; gap = (gap-1)/3 ){
for ( int j = gap; j < arr.length; j++) {
for ( int i = j; i > gap-1; i-=gap){
if ( arr[i] < arr[i-gap] ) swap(arr, i, i-gap);
}
}
}
print(arr);
}
staticvoidswap(int[] arr, int m, int n){
int temp = arr[m];
arr[m] = arr[n];
arr[n] = temp;
}
staticvoidprint(int[] arr){
System.out.println("输出数组:\n");
for ( int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}