Algs4-2.1.29希爾排序的遞增序列
阿新 • • 發佈:2018-10-27
-c 技術分享 printf out orm bsp equal \n top
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
試驗結果:不同的序列對相同元素個數與相同元素值的數組排序時,用時會不同。
![技術分享圖片](http://r.photo.store.qq.com/psb?/f33bb27a-608a-4097-811a-86bbde18cf7a/ic85vZ3FRNa6Z*rlzVsxZAUiis4d0V410xRxeS6Zixk!/o/dB8BAAAAAAAA&ek=1&kp=1&pt=0&bo=awItA2sCLQMDEDU!&tl=1&su=0164890849&tm=1540598400&sce=0-12-12&rf=2-9)
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);
}
}
}
試驗結果:不同的序列對相同元素個數與相同元素值的數組排序時,用時會不同。
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);
}
}
}
Algs4-2.1.29希爾排序的遞增序列