package com;
/**
* 实现希尔排序
* @author 小小王
*
*/
public class TestShellSort {
public static void shellSort(int[] a){
int length = a.length;
int i,j;
int h;
int temp;
for (h = length/2; h>0;h = h/2) {
for(i = h;i<length;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[] = {4,6,2,4,8,5,2,9};
shellSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
/**
* 实现希尔排序
* @author 小小王
*
*/
public class TestShellSort {
public static void shellSort(int[] a){
int length = a.length;
int i,j;
int h;
int temp;
for (h = length/2; h>0;h = h/2) {
for(i = h;i<length;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[] = {4,6,2,4,8,5,2,9};
shellSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}