博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algs4-2.3.30极端情况-各种分布排列时的快排性能
阅读量:6210 次
发布时间:2019-06-21

本文共 4645 字,大约阅读时间需要 15 分钟。

 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\n",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\n",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\n",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\n",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\n",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\n",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\n",t14.elapsedTime());
    }
}

转载于:https://www.cnblogs.com/longjin2018/p/9868591.html

你可能感兴趣的文章
MacOS Sierra升级问题小记
查看>>
在苹果MAC OS X Lion系统上使用系统自带程序配置Exchange邮箱
查看>>
易宝典文章——玩转Office 365中的Exchange Online服务 之十五 怎样管理Exchange Online的动态通讯组...
查看>>
Mysql——子查询
查看>>
最后说一声再见,以后你只存在记忆里——Windows XP
查看>>
邮件服务简单介绍
查看>>
Quantum-NSA最强大的互联网***工具
查看>>
论封闭网络的安全
查看>>
linux screen 使用方法
查看>>
令人失望的xfs文件系统
查看>>
Cent OS6.3 DHCP中继代理安装及配置
查看>>
ubuntu phantomjs安装(PhantomJS崩溃可以按这个重装解决)
查看>>
damicms
查看>>
VEEAM replication配置步骤
查看>>
关于Oracle——导入dmp文件
查看>>
Node.js(一)——NodeJs基础
查看>>
多线程-线程安全问题
查看>>
systemd.unit翻译
查看>>
python模块:doctest,unitest模块
查看>>
简单linux内核的移植实现ftp服务
查看>>