本帖最后由 依然阿邦 于 2014-7-8 19:46 编辑
把过程打印出来不就能看懂了
public class ShellText
{
public static void main (String[]args)
{
int[] arr={49,38,65,97,76,13,27,49,78,34,12,64,1};
System.out.println("排序前的序列为:");
speak(arr);
//希尔排序
int d=arr.length;
while(true)
{
d=d/2;
for(int x=0;x<d;x++)
{
for(int i=x+d;i<arr.length;i=i+d)
{
int temp = arr;
int j;
for(j=i-d;j>=0&&arr[j]>temp;j=j-d)
{
arr[j+d]=arr[j];
}
arr[j+d]=temp;
System.out.print("排序步骤"+" d="+d+" x="+x+" ");
speak(arr); //显示排序过程
}
}
if(d==1){
break;
}
}
System.out.println("排序之后:");
speak(arr);
}
public static void speak(int[] arr) //定义一个打印数组的方法
{
for(int x = 0;x<arr.length;x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.println(arr[x]);
}
}
}
运行结果
排序前的序列为:
49,38,65,97,76,13,27,49,78,34,12,64,1
排序步骤 d=6 x=0 27,38,65,97,76,13,49,49,78,34,12,64,1
排序步骤 d=6 x=0 1,38,65,97,76,13,27,49,78,34,12,64,49
排序步骤 d=6 x=1 1,38,65,97,76,13,27,49,78,34,12,64,49
排序步骤 d=6 x=2 1,38,65,97,76,13,27,49,78,34,12,64,49
排序步骤 d=6 x=3 1,38,65,34,76,13,27,49,78,97,12,64,49
排序步骤 d=6 x=4 1,38,65,34,12,13,27,49,78,97,76,64,49
排序步骤 d=6 x=5 1,38,65,34,12,13,27,49,78,97,76,64,49
排序步骤 d=3 x=0 1,38,65,34,12,13,27,49,78,97,76,64,49
排序步骤 d=3 x=0 1,38,65,27,12,13,34,49,78,97,76,64,49
排序步骤 d=3 x=0 1,38,65,27,12,13,34,49,78,97,76,64,49
排序步骤 d=3 x=0 1,38,65,27,12,13,34,49,78,49,76,64,97
排序步骤 d=3 x=1 1,12,65,27,38,13,34,49,78,49,76,64,97
排序步骤 d=3 x=1 1,12,65,27,38,13,34,49,78,49,76,64,97
排序步骤 d=3 x=1 1,12,65,27,38,13,34,49,78,49,76,64,97
排序步骤 d=3 x=2 1,12,13,27,38,65,34,49,78,49,76,64,97
排序步骤 d=3 x=2 1,12,13,27,38,65,34,49,78,49,76,64,97
排序步骤 d=3 x=2 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,38,64,34,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,64,49,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,64,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,64,65,49,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,49,64,65,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,49,64,65,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,49,64,65,76,78,97
排序步骤 d=1 x=0 1,12,13,27,34,38,49,49,64,65,76,78,97
排序之后:
1,12,13,27,34,38,49,49,64,65,76,78,97
|