2.1.37部分有序。编写一个测试用例 ,生成部分有序的数组,包括:
1)95%有序,其余部分为随机值;
2)所有元素和它们的正确位置的距离都不超过10;
3)5%的元素随机分布在整个数组中,剩下的数据都是有序的。
评估并验证这些输入数据对本节讨论的算法的性能的影响。
插入排序在左边有序,右边随机的情况下排序性能最差,其他情况下性能较好。
选择排序在所有情况下性能都较差。
希尔排序对所有情况性能都比较好,在左边95%有序时性能略差。

import java.util.Arrays;
public class E2d1d37
{
    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,int probabilityTYPE)
    {
        double total =0.0;
        Double[] a=new Double[N];

if (probabilityTYPE==0)
        {
            for (int t=0;t<T;t++)
            {
                LeftRandom95sorted(a);
                total+=time(alg,a);
            }
        }
        //probabilityTYPE=1 Poisson distribution
        if (probabilityTYPE==1)
        {
            for (int t=0;t<T;t++)
            {
                RightRandom95sorted(a);
                total+=time(alg,a);
            }
        }
        //probabilityTYPE=2 Geometric distribution
        if (probabilityTYPE==2)
        {
            for (int t=0;t<T;t++)
            {
                uniform5Random95sorted(a);
                total+=time(alg,a);
            }
        }
         //probabilityTYPE=3 Geometric distribution
        if (probabilityTYPE==3)
        {
            for (int t=0;t<T;t++)
            {
                distance10(a);
                total+=time(alg,a);
            }
        }
       return total;
    }//end timeRandomInput

public static void LeftRandom95sorted(Double[] a)
    {
        for (int i=0;i<a.length;i++)
             a[i]=StdRandom.uniform();
        Arrays.sort(a);
        int rate5=(int)0.05*a.length;
        for(int i=0;i<rate5;i++)
             a[i]=StdRandom.uniform();
    }
   
      public static void RightRandom95sorted(Double[] a)
    {
       
        for (int i=0;i<a.length;i++)
             a[i]=StdRandom.uniform();
        Arrays.sort(a);
        int rate95=(int)0.95*a.length;
        for(int i=rate95;i<a.length;i++)
             a[i]=StdRandom.uniform();
    }
    
     public static void uniform5Random95sorted(Double[] a)
    {
        for (int i=0;i<a.length;i++)
             a[i]=StdRandom.uniform();
        Arrays.sort(a);
        int rate5=(int)0.05*a.length;
        for(int i=0;i<rate5;i++);
             a[StdRandom.uniform(0,a.length)]=StdRandom.uniform();
    }
   
    public static void distance10(Double[] a)
    {
        for (int i=0;i<a.length;i++)
             a[i]=StdRandom.uniform();
        Arrays.sort(a);
        int distance=10;
        int postion;
        Double temp;
        for(int i=0;i<a.length-distance;i++)
        {
            postion=StdRandom.uniform(i+1,i+distance+1);
            temp=a[i];
            a[i]=a[postion];
            a[postion]=temp;
        }
       
        for(int i=a.length-1;i>=a.length-distance;i--)
        {
            postion=StdRandom.uniform(i-distance,a.length);
            temp=a[i];
            a[i]=a[postion];
            a[postion]=temp;
        }
    }   
       
    public static void main(String[] args)
    {
     
        Integer N=Integer.parseInt(args[0]);
        Integer T=Integer.parseInt(args[1]);
               
        String[] CASEmem=new String[4];
        CASEmem[0]="LeftRandom95sorted";
        CASEmem[1]="95sortedRightRandom";
        CASEmem[2]="uniform5Random95sorted";
        CASEmem[3]="distance10";

Double time;
         StdOut.printf("---Insertion---\n");
          time=timeRandomInput("Insertion",N,T,0);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Insertion",N,T,1);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Insertion",N,T,2);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[2],time);
         time =timeRandomInput("Insertion",N,T,3);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[3],time);
       
        
         StdOut.printf("---Selection---\n");
         time =timeRandomInput("Selection",N,T,0);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Selection",N,T,1);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Selection",N,T,2);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[2],time);
         time =timeRandomInput("Selection",N,T,3);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[3],time);
        
         StdOut.printf("---Shell---\n");
         time =timeRandomInput("Shell",N,T,0);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Shell",N,T,1);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Shell",N,T,2);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[2],time);
         time =timeRandomInput("Shell",N,T,3);
         StdOut.printf("For %d  double with case %25s,spend time=%.2f\n",N,CASEmem[2],time);

}
}

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

Algs4-2.1.37部分有序相关推荐

  1. echarts.common.min.js

    !function(t,e){"object"==typeof exports&&"object"==typeof module?module. ...

  2. Ubuntu 14.04 64bit上curl-7.37源码包中的sample 源码示例研究

    curl是Linux平台上(本人不考虑其他平台)很好用的一个工具软件,它有命令行,有C接口API,更常见的编程用途是php和python版本.如果在C/C++平台上使用curl,可以直接去官网 htt ...

  3. 一个班37人考进清华北大,老师发来一则短信,家长都沉默了

    不管成绩怎样,没有什么优生差生的区别.家长们知道,每一个小孩都是种子,只是每个人花期不同,有的花一开始,就绚丽绽放:而有的花, 却需要漫长的等待. 不要紧盯别人的花,不要觉得别人家的永远都是好,相信花 ...

  4. [零基础学JAVA]Java SE实战开发-37.MIS信息管理系统实战开发[JDBC](1)

    MIS信息管理系统实战开发之使用MySQL实现保存 开发背景 ID.姓名.年龄为公共信息,而学生有成绩,工人有工资 定义一个抽象类Person(ID.姓名.年龄),学生是其子类,有成绩,工人是其子类有 ...

  5. 程序员敲诈老板,或面临 37 年监禁

    ‍‍ 作者 | 祝涛 出品 | CSDN(ID:CSDNnews) 12月1日,网络设备制造商优比快(Ubiquiti)的前雇员尼古拉斯·夏普(nicholas Sharp)被捕,他被控窃取数据,并试 ...

  6. 你的神经网络不起作用的37个理由

    (图片由AI科技大本营付费下载自视觉中国) 作者 | Slav Ivanov 译者 | 吴金笛 校对 | 丁楠雅.林亦霖 编辑 | 王菁 来源 |  数据派THU(ID:DatapiTHU) [导语] ...

  7. ECCV 2018|商汤37篇论文入选,为你解读精选论文(附链接+开源资源)

    整理 | Jane 出品| AI科技大本营 [导读]9 月 8 日-14 日,每两年举办一次的 2018 欧洲计算机视觉大会(ECCV 2018)在德国慕尼黑召开,本次会议总共收到了 2439 篇有效 ...

  8. 详解CPU漏洞对机器学习的影响:几乎所有卷积层都受影响,QR分解降速37%

     作者 | Mikel Bober-Irizar 翻译 | 刘畅 编辑 | Donna (备注:KPTI 在计算机中指 Kernel page-table isolation,是一种Linux内核 ...

  9. 37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...

    欢迎关注:视学算法,每日好文章! 综合自网络 从短期来看,程序员的确算是个不错的工作,薪水也比一般岗位高很多,但是从长远来看,程序员的中年危机会比其他岗位来的更早,很多程序员只有到了35岁左右,才能真 ...

最新文章

  1. 用Java创建JMeter变量 - 终极指南
  2. python流程控制-python之流程控制
  3. 计算图片的宽和高 动态设置图片的位置
  4. 实例化Spring Bean:Bean实例化的姿势有多少种?
  5. 单调栈 leetcode整理(一)
  6. Nvidia Jetson TX2入门指南(白话版)
  7. 谷粒商城集群篇爬坑笔记--Gitee拉取项目报错、项目target文件不存在(部分项目不全)、SonarQube报错
  8. Linux输入子系统浅析
  9. easyui-textbox锁定按钮不锁定_刘诗雯锁定世界杯参赛资格!孙颖莎不满足要求,无缘对阵伊藤美诚...
  10. windows 启动 cmd快捷键,类似于 linux “Ctrl+Alt+T“ 启动终端
  11. 如何判断绝缘接头质量的好坏?
  12. 让Excel窗口保持在所有窗口前面
  13. java工程师青春饭吗_Java工程师是青春饭吗?
  14. 第一次用python写爬虫
  15. mysql8更改区分大小写_mysql 8 大小写修改问题
  16. CodeForces 714C Sonya and Queries
  17. android硬解码x265,Android 设置硬解码 h265 失败
  18. 软件工程学习之小学四则混合运算出题软件 Version 1.00 设计思路及感想
  19. 工控机组建文件服务器,工控机做云服务器
  20. 技能篇:awk教程-linux命令

热门文章

  1. python三十六:shelve模块
  2. JQuery-FullCalendar 多数据源实现日程展示
  3. lighttpd防御 Slow HTTP Denial of Service Attack 解决办法
  4. 【297天】我爱刷题系列056(2017.11.29)
  5. SQL-Oracle游标
  6. 阿里云产品搭建web应用梳理
  7. 在linux系统上运行新加的内核模块(驱动模块) 需要安装的东西
  8. SDRAM芯片初始化、行有效、列读写时序(高手进阶,终极内存技术指南——完整/进阶版) ——本文为转载...
  9. s3c6410 uboot代码分析《一》
  10. ThinkPHP 框架培训资料