标签:shel ted amp com shell ESS .com print 存储
2.1.11将希尔排序中实时计算递增序列改为预先计算并存储在一个数组中。
public class Shell2
{
public static void sort(Comparable[] a)
{
int N=a.length;
int h=1;
int[] SN=new int[20];
int hMaxIndex=0;
while (h<N/3)
{
SN[hMaxIndex]=h;
h=3*h+1;
hMaxIndex++;
}
for (int hIndex=hMaxIndex;hIndex>0;hMaxIndex--)
{
h=SN[hIndex];
for (int i=h;i<N;i++)
{
for (int j=i;j>=h && less(a[j],a[j-h]);j-=h)
exch(a,j,j-h);
}
}
}
private static boolean less(Comparable v,Comparable w)
{ return v.compareTo(w)<0;}
private static void exch(Comparable[] a,int i,int j)
{
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a)
{
for (int i=0;i<a.length;i++)
StdOut.print(a[i]+" ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{
for (int i=0;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
}
标签:shel ted amp com shell ESS .com print 存储
原文地址:https://www.cnblogs.com/longjin2018/p/9860018.html