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. 31 python下实现并发编程

    一 背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所 ...

  2. GUID在.net里的使用

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  3. 剑指offer--6.数值的整数次方

    时间限制:1秒 空间限制:32768K 热度指数:362909 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class S ...

  4. CANopenSocket CANopenCommand.c hacking

    /***************************************************************************** * CANopenSocket CANop ...

  5. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  6. 三、python沉淀之路--列表(list)

    一.列表提供的一些方法 1.append():在原值最后追加 li = [11,22,33,44,55,] li.append(99) print(li) li.append('中国') print( ...

  7. angular.run 妙用

    **1.浏览器判断**在angular做微信应用的时候,有时候我们也想把相同一份代码运行在非微信的浏览器上,这时候我们可以在angular的run上写点东西实现~例如asw.run函数里执行定义一个$ ...

  8. BZOJ1116:[POI2008]CLO

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  9. 安装成功的nginx如何添加未编译模块?

    在重启nginx后发生了错误,错误如下: nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /u ...

  10. rtsp/rtp over http

    转载:http://linux-expert.blog.163.com/blog/static/764585292008530912712/ rtsp/rtp over http C->S (g ...