zoukankan      html  css  js  c++  java
  • Algs4-2.1.31双倍测试

    2.1.31双倍测试。编写一个能够对排序算法进行双倍测试的用例。数组规模N的起始值为1000,排序后打印N、估计排序用时、实际排序用时以及在N增倍之后两次用时的比例。用这段程序验证在随机输入模型下插入排序和选择排序的运行时间都是平方级别的。对希尔排序的性能作出猜想并验证你的猜想。
    图片
    public class SortCompare
    {
        public static double time (String alg,Double[] a)
        {
            Stopwatch timer =new Stopwatch();
            if(alg.equals("Insertion")) Insertion.sort(a);
            if(alg.equals("Selection")) Selection.sort(a);
            if(alg.equals("Shell")) Shell.sort(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 %s is",N,alg1);
            StdOut.printf(" %.2f times faster than %s ",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 N=1000;N<1000000000;N=2*N)
            {
             prevT1=thisT1;
             prevT2=thisT2;
             prevT3=thisT3;
              //
             thisT1=timeRandomInput("Insertion",N,1);
             thisT2=timeRandomInput("Selection",N,1);
             thisT3=timeRandomInput("Shell",N,1);
             //
            StdOut.printf("---For %d random Doubles ",N);
            StdOut.printf("Insertion use time=%.2f ,tihisTime/prevTime=%.2f ",thisT1,thisT1/prevT1);
            StdOut.printf("Selection use time=%.2f ,tihisTime/prevTime=%.2f ",thisT2,thisT2/prevT2);
            StdOut.printf("Shell     use time=%.2f ,tihisTime/prevTime=%.2f ",thisT3,thisT3/prevT3);
            }
        }
    }

  • 相关阅读:
    Kubernetes集群管理工具kubectl命令技巧大全
    LVM 逻辑卷扩容
    制作 Ubuntu 16.04 离线apt源
    Kubernetes 存储简介
    处理K8S PVC删除后pod报错
    处理 K8S Orphaned pod found
    Kubernetes之GlusterFS集群文件系统高可用安装,提供动态卷存储
    Linux安装MYSQL并部署主从复制集群
    CentOS 7.4通过rpm包离线安装 Mysql8.0并部署主从复制(附从库备份脚本)
    容器化安装Mysql 8.0 并部署主从复制
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860058.html
Copyright © 2011-2022 走看看