package com.donghao.shellSort;
public class ShellSort {
public static void main(String[] args){
int maxSize = 10;
ArraySh arr;
arr = new ArraySh(maxSize);
for(int i=0;i<10;i++){
long n = (int)(Math.random() * 100);
arr.insert(n);
}
System.out.println("排序前");
arr.display();
System.out.println();
arr.shellSort();
System.out.println("排序后");
arr.display();
}
}
package com.donghao.shellSort;
public class ArraySh {
private long[] theArray;
private int nElems;
public ArraySh(int max){
theArray = new long[max];
nElems = 0;
}
public void insert(long value){
theArray[nElems++] = value;
}
public void display(){
for(int i=0;i<nElems;i++)
System.out.print(theArray[i] + " ");
}
public void shellSort(){
int inner,outer;
long temp;
int h=1;
while(h<=nElems/3)
h=h*3 +1;
while(h>0){
for(outer = h;outer<nElems;outer++){
temp = theArray[outer];
inner = outer;
while(inner>h-1 && theArray[inner-h] >= temp){
theArray[inner] = theArray[inner -h];
inner -=h;
} //end while
theArray[inner] = temp;
} //end for
h = (h-1) /3;
}
}
}