2.3.30极端情况。用初始随机化和非初始随机化的快速排序测试练习2.1.35和练习2.1.36中描述的大型非随机数组。在将这些大数组排序时,乱序对快速排序的性能有何影响?
public class E2d3d30
{
public static void sort(Comparable[] a,boolean isRandom)
{
if (isRandom) StdRandom.shuffle(a);
sort(a,0,a.length-1);
}
private static void sort(Comparable[] a,int lo,int hi)
{
if (hi<=lo) return;
int j=partition(a,lo,hi);
sort(a,lo,j-1);
sort(a,j+1,hi);
}
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 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]);
//1)高斯分布
Double[] arrayGaussianRandom=new Double[N];
Double[] arrayGaussian=new Double[N];
for(int i=0;i<N;i++)
{
arrayGaussianRandom[i]=StdRandom.gaussian();
arrayGaussian[i]=arrayGaussianRandom[i];
}
Stopwatch t1=new Stopwatch();
sort(arrayGaussianRandom,true);
StdOut.printf("1)%10s, N=%d, Shuffle elapsedTime=%.3f","Gaussian",N,t1.elapsedTime());
Stopwatch t2=new Stopwatch();
sort(arrayGaussian,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t2.elapsedTime());
//2)泊松分布
Double[] arrayPoissonRandom=new Double[N];
Double[] arrayPoisson=new Double[N];
for(int i=0;i<N;i++)
{
arrayPoissonRandom[i]=1.0*StdRandom.poisson(0.5);
arrayPoisson[i]=arrayPoissonRandom[i];
}
Stopwatch t3=new Stopwatch();
sort(arrayPoissonRandom,true);
StdOut.printf("2)%10s, N=%d, Shuffle elapsedTime=%.3f","Poisson",N,t3.elapsedTime());
Stopwatch t4=new Stopwatch();
sort(arrayPoisson,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t4.elapsedTime());
//3)几何分布
Double[] arrayGeometricRandom=new Double[N];
Double[] arrayGeometric=new Double[N];
for(int i=0;i<N;i++)
{
arrayGeometricRandom[i]=1.0*StdRandom.geometric(0.5);
arrayGeometric[i]=arrayGeometricRandom[i];
}
Stopwatch t5=new Stopwatch();
sort(arrayGeometricRandom,true);
StdOut.printf("3)%10s, N=%d, Shuffle elapsedTime=%.3f","Geometric",N,t5.elapsedTime());
Stopwatch t6=new Stopwatch();
sort(arrayGeometric,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t6.elapsedTime());
//4)离散分布
Double[] arrayDiscreteRandom=new Double[N];
Double[] arrayDiscrete=new Double[N];
double[] p1 = { 0.5, 0.3, 0.1, 0.1 };
for(int i=0;i<N;i++)
{
arrayDiscreteRandom[i]=1.0*StdRandom.discrete(p1);
arrayGeometric[i]=arrayDiscreteRandom[i];
}
Stopwatch t7=new Stopwatch();
sort(arrayDiscreteRandom,true);
StdOut.printf("4)%10s, N=%d, Shuffle elapsedTime=%.3f","Discrete",N,t7.elapsedTime());
Stopwatch t8=new Stopwatch();
sort(arrayGeometric,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t8.elapsedTime());
//5)一半是0一半是1
Integer[] array01Random=new Integer[N];
Integer[] array01=new Integer[N];
for(int i=0;i<N;i++)
{
if(i<N/2)
array01Random[i]=0;
else
array01Random[i]=1;
array01[i]=array01Random[i];
}
Stopwatch t9=new Stopwatch();
sort(array01Random,true);
StdOut.printf("5)%10s, N=%d, Shuffle elapsedTime=%.3f","half01",N,t9.elapsedTime());
Stopwatch t10=new Stopwatch();
sort(array01,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t10.elapsedTime());
//6)一半是0一半是1
Integer[] array012Random=new Integer[N];
Integer[] array012=new Integer[N];
for(int i=0;i<N/2;i++)
{
array012Random[i]=0;
array012[i]=0;
}
for(int i=N/2;i<3/4*N;i++)
{
array012Random[i]=1;
array012[i]=1;
}
for(int i=3/4*N;i<N;i++)
{
array012Random[i]=2;
array012[i]=2;
}
Stopwatch t11=new Stopwatch();
sort(array012Random,true);
StdOut.printf("6)%10s, N=%d, Shuffle elapsedTime=%.3f","half012",N,t11.elapsedTime());
Stopwatch t12=new Stopwatch();
sort(array012,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t12.elapsedTime());
//7)一半是0一半是随机int
Integer[] array0intRandom=new Integer[N];
Integer[] array0int=new Integer[N];
for(int i=0;i<N;i++)
{
if(i<N/2)
array0intRandom[i]=0;
else
array0intRandom[i]=StdRandom.uniform(-10000000,10000000);
array0int[i]=array0intRandom[i];
}
Stopwatch t13=new Stopwatch();
sort(array0intRandom,true);
StdOut.printf("7)%10s, N=%d, Shuffle elapsedTime=%.3f","half0int",N,t13.elapsedTime());
Stopwatch t14=new Stopwatch();
sort(array0int,false);
StdOut.printf(" ,unShuffle elapsedTime=%.3f
",t14.elapsedTime());
}
}