import java.util.Random;
public class ShellSortApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int maxSize=12;
ArraySh arr=new ArraySh(maxSize);
for(int i=0;i<maxSize;i++){
int num=new Random().nextInt(100);
arr.insert(num);
}
arr.display();
arr.shellSort();
arr.display();
}
}
class ArraySh{
private int[] theArray;
private int nElems; //数据的数量
public ArraySh(int max) {
this.theArray=new int[max];
nElems=0;
}
public void insert(int value){
theArray[nElems++]=value;
}
public void display(){
for(int i:theArray){
System.out.print(i+" ");
}
System.out.println();
}
public void shellSort(){
int h=1;//h为间隔
while(h<nElems/3){
h=h*3+1;
}
while(h>0){
for(int outter=h;outter<nElems;outter++){//outter用来标示循环开始的位置
int temp=theArray[outter];
int inner=outter;//inner 用来标示这个数最后停留的位置
while(inner>h-1&&theArray[inner-h]>temp){//inner>h-1用来标示循环停止的条件
theArray[inner]=theArray[inner-h];
inner-=h;
}
theArray[inner]=temp;
}
h=(h-1)/3;
}
}
}