您的位置:首页 > 运维架构 > Shell

shell(缩小增量)排序

2017-01-07 14:32 169 查看
package coding;

public class cha04_shellSort {
static final int SIZE=10;
public static void shellSort(int[] a){
int i,j, h;
int r,temp;
int x=0;
for(r=a.length/2;r>=1;r/=2){
for(i=r;i<a.length;i++){
temp=a[i];
j=i-r;
while(j>=0&&temp<a[j]){
a[j+r]=a[j];
j-=r;
}
a[j+r]=temp;

}
x++;
System.out.print("第"+x+"步排序结果是:");
for(h=0;h<a.length;h++){
System.out.print(" "+a[h]);
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] shuzu=new int[SIZE];
int i;
for(i=0;i<SIZE;i++){
shuzu[i]=(int)(100+Math.random()*(100+1));
}
System.out.print("排序前的数组:");
for(i=0;i<SIZE;i++){

System.out.print(shuzu[i]+" ");
}
System.out.println();
shellSort(shuzu);
System.out.print("排序后的数组为:");
for(i=0;i<SIZE;i++){
System.out.print(shuzu[i]+" ");
}
System.out.println();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息