2.1.29希尔排序的递增序列。通过实验比较算法2.3中所使用的递增序列和递增序列1,5,19,41,109,209,505,929,2161,3905,8929,16001,36289,64769,146305,260609(这是通过序列9*4^k-9*2^k+1和4^k-3*2^k+1综合得到的)。可以参考练习2.1.11。2.3节中前16个序列为1,4,13,40,121,364,1093,3280,9841,29524,88573,265720,797161,2391484,7174453,21523360
试验结果:不同的序列对相同元素个数与相同元素值的数组排序时,用时会不同。
public class Shell2
{
public static void sort1(Comparable[] a)
{
int N=a.length;
int[] SN={1,4,13,40,121,364,1093,3280,9841,29524,88573,265720,797161,2391484,7174453,21523360};
int h;
for (int hIndex=SN.length-1;hIndex>0;hIndex--)
{
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);
}
}
}
public static void sort2(Comparable[] a)
{
int N=a.length;
int[] SN={1,5,19,41,109,209,505,929,2161,3905,8929,16001,36289,64769,146305,260609};
int h;
for (int hIndex=SN.length-1;hIndex>0;hIndex--)
{
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;
}
}
public class SortCompare
{
public static double time (String alg,Double[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.sort(a);
//exercise2.1.24
if(alg.equals("Insertion2")) Insertion2.sort(a);
//exercise2.1.25
if(alg.equals("Insertion3")) Insertion3.sort(a);
//exercise2.1.26
if(alg.equals("Insertion4")) Insertion3.sort(a);
if(alg.equals("Selection")) Selection.sort(a);
if(alg.equals("Shell1")) Shell2.sort1(a);
if(alg.equals("Shell2")) Shell2.sort2(a);
// if(alg.equals("Merge")) Merge.sort(a);
// if(alg.equals("Quick")) Quick.sort(a);
// if(alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static double timeRandomInput(String alg,int N,int T)
{
double total =0.0;
Double[] a=new Double[N];
for (int t=0;t<T;t++)
{
for (int i=0;i<N;i++)
a[i]=StdRandom.uniform();
total+=time(alg,a);
}
return total;
}//end timeRandomInput
public static void main(String[] args)
{
/*
String alg1=args[0];
String alg2=args[1];
int N=Integer.parseInt(args[2]);
int T=Integer.parseInt(args[3]);
double t1=timeRandomInput(alg1,N,T);
double t2=timeRandomInput(alg2,N,T);
StdOut.printf("For %d random Doubles\n %s is",N,alg1);
StdOut.printf(" %.2f times faster than %s\n",t2/t1,alg2);
*/
double prevT1=0.0;
double prevT2=0.0;
double prevT3=0.0;
double thisT1=0.0;
double thisT2=0.0;
double thisT3=0.0;
for (int i=128;i<1000000000;i=2*i)
{
prevT1=thisT1;
prevT2=thisT2;
prevT3=thisT3;
//
Double[] a=new Double[i];
Double[] aCopy=new Double[i];
for (int j=0;j<i;j++)
{
a[j]=StdRandom.uniform();
aCopy[j]=a[j];
}
//
thisT1=time("Shell1",a);
thisT2=time("Shell2",aCopy);
//thisT3=timeRandomInput("Shell",i,1);
//
StdOut.printf("---For %d random Doubles\n",i);
StdOut.printf("Shell 1 4 13 40... use time=%.2f ,tihisTime/prevTime=%.2f\n",thisT1,thisT1/prevT1);
StdOut.printf("Shell 1,5,19,41... use time=%.2f ,tihisTime/prevTime=%.2f\n",thisT2,thisT2/prevT2);
StdOut.printf("SN(1,4,13,40...) use time/(1,5,19,41...) use time=%.2f\n",thisT1/thisT2);
// StdOut.printf("Shell use time=%.2f ,tihisTime/prevTime=%.2f\n",thisT3,thisT3/prevT3);
}
}
}