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. RESTful 组件

    1. CBV FBV: url("index/",index) # index(request) url("index/(\d+)",index) # inde ...

  2. Linux C 编程内存泄露检测工具(一):mtrace

    前言 所有使用动态内存分配(dynamic memory allocation)的程序都有机会遇上内存泄露(memory leakage)问题,在Linux里有三种常用工具来检测内存泄露的情況,包括: ...

  3. C语言编译全过程

    编译的概念:编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序.    ...

  4. XE7 - ListView自测笔记

    这两天主要是摸索着使用了ListView和SQLite.郁闷过,也有收获. 一.SQLite 首先记录下SQLite自己碰到的几个小问题: 1. SQLite中字符串连接符是‘||’, 换行符为 x' ...

  5. CodeForces 589H Tourist Guide

    传送门 题目大意 给定$n$个点$m$条边的无向图,有$K$个关键点,你要尽可能的让这些关键点两两匹配,使得所有点对之间可以通过简单路径连接且任意两个简单路径没有重复的边(可以是共同经过一个点),输出 ...

  6. CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)

    Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...

  7. C#获取堆栈信息,输出文件名、行号、函数名、列号等

    命名空间:System.Diagnostics 得到相关信息: StackTrace st = new StackTrace(new StackFrame(true));StackFrame sf = ...

  8. 笔记:加 ly 不一定是副词

    笔记:加 ly 不一定是副词 加 ly 变副词,但有些单词以 ly 结尾,长得像副词,却是形容词. costly = cost + ly a costly item. 一件昂贵的物品. lovely ...

  9. angular : copy vs extend

    While using AngularJS, we come across some situation in which we need to copy one object to another ...

  10. 蓝桥杯 基础练习 BASIC-14 时间转换

    基础练习 时间转换   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个以秒为单位的时间t,要求用“<H>:<M>:<S>”的格式来表示这个时间 ...