2.3.29随机化。用经验性的研究对比随机选择切分元素和正文所述的一开始就将数组随机化这两处策略的效果。在子数组大小为M时进行切换,将大小为N的不重复数组排序,其中M=10、20和50,N=10^3、10^4、10^5和10^6。
答:由于生成的待排序数组本身已是随机化,方式1在排序前打乱一次数组需要花费时间,所以方式1比方式2使用的排序时间要长一些,所以待排序数组本身已是随机化时两种方法比较性不是特别强。
public class E2d3d29
{
public static void main(String[] args)
{
int Nlen[]={1000,10000,100000,1000000};
int Mlen[]={10,20,50};
for(int Ni=0;Ni<Nlen.length;Ni++)
{
int N=Nlen[Ni];
for (int Mi=0;Mi<Mlen.length;Mi++)
{
int M=Mlen[Mi];
double s1Times=0.0;
double s2Times=0.0;
for (int time=0;time<10;time++)
{
Double[] a1=new Double[N];
Double[] a2=new Double[N];
for(int i=0;i<N;i++)
{
a1[i]=StdRandom.random();
a2[i]=a1[i];
}
Stopwatch timer1=new Stopwatch();
E2d3d29s1.sort(a1,M);
s1Times=s1Times+timer1.elapsedTime();
//
Stopwatch timer2=new Stopwatch();
E2d3d29s2.sort(a2,M);
s2Times=s2Times+timer2.elapsedTime();
}
StdOut.printf("N=%7d, M=%2d, shuffleElapsedTime=%.3f,
randomElapsedTime=%.3f,
shuffle-random=%.3f
",N,M,s1Times/10,s2Times/10,(s1Times+s2Times)/10);
}
}//end for Ni
}//end main
}//end class
public class E2d3d29s1
{
public static void sort(Comparable[] a,int M)
{
StdRandom.shuffle(a);
sort(a,0,a.length-1,M);
}
private static void sort(Comparable[] a,int lo,int hi,int M)
{
if (hi-lo+1<M)
{
InsertSort(a,lo,hi);
return;
}
int j=partition(a,lo,hi);
sort(a,lo,j-1,M);
sort(a,j+1,hi,M);
}
private static int partition(Comparable[] a,int lo,int hi)
{
int i=lo,j=hi+1;
Comparable v=a[lo];
while(true)
{
while(less(a[++i],v)) if(i==hi) break;
while(less(v,a[--j])) if(j==lo) break;
if(i>=j) break;
exch(a,i,j);
}
exch(a,lo,j);
return j;
}
private static void InsertSort(Comparable[] a,int lo,int hi)
{
for (int i=lo+1;i<hi+1;i++)
{
for (int j=i;j>0 && less(a[j],a[j-1]);j--)
exch(a,j,j-1);
}
}
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=1;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int k=0;k<N;k++)
a[k]=StdRandom.random();
sort(a,10);
StdOut.println("isSorted="+isSorted(a));
}
}
public class E2d3d29s2
{
public static void sort(Comparable[] a,int M)
{
sort(a,0,a.length-1,M);
}
private static void sort(Comparable[] a,int lo,int hi,int M)
{
if (hi-lo+1<M)
{
InsertSort(a,lo,hi);
return;
}
int j=partition(a,lo,hi);
sort(a,lo,j-1,M);
sort(a,j+1,hi,M);
}
private static int partition(Comparable[] a,int lo,int hi)
{
int i=lo,j=hi+1;
exch(a,lo,StdRandom.uniform(lo,hi+1));
Comparable v=a[lo];
while(true)
{
while(less(a[++i],v)) if(i==hi) break;
while(less(v,a[--j])) if(j==lo) break;
if(i>=j) break;
exch(a,i,j);
}
exch(a,lo,j);
return j;
}
private static void InsertSort(Comparable[] a,int lo,int hi)
{
for (int i=lo+1;i<hi+1;i++)
{
for (int j=i;j>0 && less(a[j],a[j-1]);j--)
exch(a,j,j-1);
}
}
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=1;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int k=0;k<N;k++)
a[k]=StdRandom.random();
sort(a,10);
StdOut.println("isSorted="+isSorted(a));
}
}