import java.util.Arrays;

 public class Solution
 {
     public static void main(String[] args)
     {
         int[] array1 = new int[10000];
         int[] array2 = new int[10000];

         for(int i = 0; i < 10000; i++)
         {
             array1[i] = (int)(Math.random() * 100);
             array2[i] = (int)(Math.random() * 100);
         }

         int sign1 = (int)(Math.random() * 100);
         int sign2 = (int)(Math.random() * 100);

         System.out.println("Array1 run time: " + linearRunTime(array1, sign1));
         System.out.println("Array2 run time: " + binaryRunTime(array2, sign2));
     }

     public static long linearRunTime(int[] array, int number)
     {
         long startTime = System.currentTimeMillis();
         linearSearch(array, number);
         long endTime = System.currentTimeMillis();
         long executionTime = endTime - startTime;
         return executionTime;
     }

     public static long binaryRunTime(int[] array, int number)
     {
         Arrays.sort(array);
         long startTime = System.currentTimeMillis();
         binarySearch(array, number);
         long endTime = System.currentTimeMillis();
         long executionTime = endTime - startTime;
         return executionTime;
     }

     public static void linearSearch(int[] array, int number)
     {
         for(int i = 0; i < array.length; i++)
             if(number == array[i])
                 return;
     }

     public static void binarySearch(int[] array, int number)
     { binarySearch(array, number, 0, array.length - 1); }

     public static void binarySearch(int[] array, int number, int low, int high)
     {
         if(low < high)
             return;
         int mid = (low + high) / 2;
         if(number < array[mid])
             binarySearch(array, number, low, mid);
         else if(number > array[mid])
             binarySearch(array, number, mid + 1, high);
         else if(number == array[mid])
             return;
     }
 }

HW6.16的更多相关文章

  1. javaweb基础(16)_jsp指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  2. 在Ubuntu 16.10安装mysql workbench报未安装软件包 libpng12-0错误

    1.安装mysql workbench,提示未安装软件包 libpng12-0 下载了MySQL Workbench 6.3.8   在安装的时候报错: -1ubu1604-amd64.deb 提示: ...

  3. Ubuntu 16.10 安装KolourPaint 4画图工具

    KolourPaint 4画图工具简单实用,可以绘画.视频处理和图标编辑: • 绘画:绘制图表和“手绘” • 视频处理:编辑截图和照片;应用特效 • 图标编辑:绘画剪贴和标识透明化 1.在Ubuntu ...

  4. 真正的汉化-PowerDesigner 16.5 汉化

    一.背景 经常使用PowerDesigner,之前使用15版本,后来16出来后,就一直在使用16,不过一直是英文.一些同事对使用英文版总显示有些吃力. 遍寻百度.必应,都没有找到真正的针对版本16的汉 ...

  5. Win7安装MySQL-5.7.16过程

    1.在C盘新建MYSQL文件夹:2.将mysql-5.7.16-winx64拷贝到C:\MYSQL文件夹下,更名为mysql-5.7.16:3.在mysql-5.7.16目录下,建my.ini文件,内 ...

  6. 使用 GCC 和 GNU Binutils 编写能在 x86 实模式运行的 16 位代码

    不可否认,这次的标题有点长.之所以把标题写得这么详细,主要是为了搜索引擎能够准确地把确实需要了解 GCC 生成 16 位实模式代码方法的朋友带到我的博客.先说一下背景,编写能在 x86 实模式下运行的 ...

  7. 企业IT管理员IE11升级指南【16】—— 使用Compat Inspector快速定位IE兼容性问题

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  8. ABP(现代ASP.NET样板开发框架)系列之16、ABP应用层——数据传输对象(DTOs)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之16.ABP应用层——数据传输对象(DTOs) ABP是“ASP.NET Boilerplate Project ...

  9. 安装MYSQL详细教程 版本:mysql-installer-community-5.7.16.0 免安装版本和安装版本出现错误的解决

    一.版本的选择 之前安装的Mysql,现在才来总结,好像有点晚,后台换系统了,现在从新装上Mysql,感觉好多坑,我是来踩坑,大家看到坑就别跳了,这样可以省点安装时间,这个折腾了两天,安装了好多个版本 ...

随机推荐

  1. use sql trigger call java function

    Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...

  2. 深入理解计算机各种类型大小(sizeof)

    深入理解计算机各种类型大小(sizeof)   // Example of the sizeof keyword size_t  i = sizeof( int ); struct align_dep ...

  3. 获取Android系统时间

    目的: 输入 2014-09-09 14:02:03    输出 等待:1小时20分 注意: HH:mm:ss 为获取手机 24小时格式的时间  15:03    hh:mm:ss 为12小时模式的时 ...

  4. PJSIP在windows(xp或者win7)下的编译,编译工具是vs2008,PJSIP版本2.3

    PJSIP是一个开源的SIP协议库,它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多媒体通信框架提供了非常清晰的API,以及NAT穿越的功能.PJSIP具有 ...

  5. 阿里巴巴Java面试题

    研二是需要找实习的时候了,因阿里有同学内推就直接参加了电话面试,不说其他的废话直接上问题,阿里的面试官还是不错的,和蔼可亲,为人谦虚,大牛什么都懂.(投的职位是java研发)1.java中所有类的父类 ...

  6. 安装Hadoop系列 — 安装Eclipse

    1.下载 Eclipse从 http://www.eclipse.org/downloads/index-developer.php下载合适版本,如:Eclipse IDE for C/C++ Dev ...

  7. Android 签名(6)编译时源码的签名

    1,使用源码中的默认签名 在源码中编译一般都使用默认签名的,在某源码目录中用运行下面命令能看到签名命令. $ mm showcommands Android提供了签名的程序signapk.jar,用法 ...

  8. Extension Method[上篇]

    在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...

  9. poj2750Potted Flower (线段树)

    http://poj.org/problem?id=2750 之前做过类似的题 把一段的左连续最大.最小 右连续最大及最小及中间的连续更新出 就可以算出这段最大的连续和 注意不能全部加上 加上一特判 ...

  10. [原]Unity3D深入浅出 - 物理引擎之碰撞体(Colliders)

    通常Colliders会与Rigidbody一起使用,没有添加碰撞体的刚体会彼此相互穿过. 常用碰撞体有以下几种: Box Collider:盒子碰撞体,是一个立方体外形的碰撞体,可调整为不同大小的长 ...