package algorithms.analysis14;

 import algorithms.util.StdOut;
import algorithms.util.StdRandom; /******************************************************************************
* Compilation: javac BitonicMax.java
* Execution: java BitonicMax N
* Dependencies: StdOut.java
*
* Find the maximum in a bitonic array (strictly increasing, then strictly
* decreasing) of size N in log N time.
*
* % java BitonicMax N
*
******************************************************************************/ public class BitonicMax { // create a bitonic array of size N
public static int[] bitonic(int N) {
int mid = StdRandom.uniform(N);
int[] a = new int[N];
for (int i = 1; i < mid; i++) {
a[i] = a[i-1] + 1 + StdRandom.uniform(9);
} if (mid > 0) a[mid] = a[mid-1] + StdRandom.uniform(10) - 5; for (int i = mid + 1; i < N; i++) {
a[i] = a[i-1] - 1 - StdRandom.uniform(9);
} for (int i = 0; i < N; i++) {
StdOut.println(a[i]);
}
return a;
} // find the index of the maximum in a bitonic subarray a[lo..hi]
public static int max(int[] a, int lo, int hi) {
if (hi == lo) return hi;
int mid = lo + (hi - lo) / 2;
if (a[mid] < a[mid + 1]) return max(a, mid+1, hi);
if (a[mid] > a[mid + 1]) return max(a, lo, mid);
else return mid;
} public static void main(String[] args) { int N = Integer.parseInt(args[0]);
int[] a = bitonic(N);
StdOut.println("max = " + a[max(a, 0, N-1)]);
}
}

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  2. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

  3. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-007按位置,找出数组相关最大值

    Given an array a[] of N real numbers, design a linear-time algorithm to find the maximum value of a[ ...

  4. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存

    1. 2. 3.字符串

  5. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理

    1. 2. 3. 4. 5. 6.

  6. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤

    For many programs, developing a mathematical model of running timereduces to the following steps:■De ...

  7. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

随机推荐

  1. 计算机_软件技巧_01_优雅地再word中插入代码

    二.参考资料 1.如何优雅的在 Microsoft word中插入代码

  2. https页面 和 http请求的问题

    (1)强制升级http 静态资源地址为https地址 https页面中不能使用http请求,http页面中可以使用https请求. 关于在https 页面有一些http的请求,可以在<head& ...

  3. 使用kaptcha验证码组件操作演示

    1.创建一个Maven项目 2.在pom.xml中引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  4. Webpack之“多页面开发”最佳实战

    前言:相信之前看过这篇文章,前端构建工具之“Webpack”的朋友,对于Webpack有了一定的了解.那么今天就跟大家分享下:如何利用webpack,来进行多页面项目实战开发. 一.项目初始化安装 1 ...

  5. 导入镜像后,容器内httpd起不来

    导入镜像后发现bash进程为1 与之前apache启动的进程冲突了 解决办法:删除apache进程号,通过apachectl重启apache进程

  6. 用Json Template在Azure上创建Cisco CSR路由器

    Azure的ARM模式可以通过Json的模板创建VM.本文以Cisco的CSR的image为例,介绍如何用Json的创建VM. 一.Cisco CSR的Image 首先把Cisco CSR的image ...

  7. Hibernate基础(一)

    Hibernate是ORM开源组件 源码:http://sourceforge.net/projects/hibernate/ 1.Hibernate的配置文件. 默认配置文件: hibernate. ...

  8. binlog之一:binary log初探

    MySQL Binary Log也就是常说的bin-log,是mysql执行改动产生的二进制日志文件,其主要作用有两个: Replication(主从数据库):在master端开启binary log ...

  9. L2-014. 列车调度(set的使用,最长递增子序列)

    L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...

  10. JAVA方法中的参数用final来修饰的效果

    很多人都说在JAVA中用final来修饰方法参数的原因是防止方法参数在调用时被篡改,其实也就是这个原因,但理解起来可能会有歧义,我们需要注意的是,在final修饰的方法参数中,如果修饰的是基本类型,那 ...