2.1.12令希尔排序打印出递增序列的每个元素所带来的比较次数和数组大小的比值。编写一个测试用例对随机Double数组进行希尔排序,验证该值是一个小常数,数组大小按照10的冪次递增,不小于100。
public class E2d1d12
{
public static int compareTimes=0;
public static int maxRate=0;
public static void sort(Comparable[] a)
{
int N=a.length;
int h=1;
while (h<N/3) h=3*h+1;
while (h>=1)
{
compareTimes=0;
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);
}
if(compareTimes/N>maxRate) maxRate= compareTimes/N;
StdOut.printf("N=%d,h=%d,compareTimes=%d,rate=%d\n",N,h,compareTimes,compareTimes/N);
h=h/3;
}
}
private static boolean less(Comparable v,Comparable w)
{
compareTimes++;
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 static void main(String[] args)
{
for ( int N=100;N<=10000000;N=N*10)
{
Double[] a=new Double[N];
for (int i=0;i<N;i++)
a[i]=StdRandom.uniform(100.0,100000000.0);
sort(a);
}
StdOut.println("maxRate=" + maxRate);
}
}
转载于:https://www.cnblogs.com/longjin2018/p/9860019.html