</pre><p><pre class="java" name="code">package cn.hncu.sortMethods;
public class shellSort {
public static void main(String[] args) {
int a[]={2,2,5,6,12,4,29,1};
shellSort1(a);
print(a);
}
private static void print(int[] a) {
for(int num:a){
System.out.print(num+"\t");
}
System.out.println();
}
private static void shellSort1(int[] a) {
for(int gap=(a.length+1)/2;gap>0;){//分组
for(int i=0;i<a.length-gap;i++){//循环次数
for(int j=i;j<a.length-gap;j+=gap){
if(a[j]>a[j+gap]){
swap(a,j,j+gap);
}
}
}
if(gap>1){
gap=(gap+1)/2;
}else if(gap==1){
break;
}
}
}
private static void swap(int[] a, int i, int j) {
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}