算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax
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的更多相关文章
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
- 算法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[ ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存
1. 2. 3.字符串
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理
1. 2. 3. 4. 5. 6.
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
For many programs, developing a mathematical model of running timereduces to the following steps:■De ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
随机推荐
- jquery判断密码是否一致?
密码 请输入密码 重新输入密码 请输入新密码 <input type="text" id="btn0"> 密码 <span class=&qu ...
- POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)
NYOJ的数据水一点,POJ过了是真的过了 /* 拓扑排序模板题: 每次输入都要判断有环与有序的情况,如果存在环路或者已经有序可以输出则跳过下面的输入 判断有序,通过是否在一个以上的入度为0的点,存在 ...
- xdebug的安装测试
1.下载 php -version PHP 7.2.0 (cli) (built: Dec 7 2017 23:07:46) ( NTS DEBUG ) 如果PHP版本是7.2以上的必须要下载Xdeb ...
- checking for event2/thread.h... no libevent_pthreads required, failing
/********************************************************************************** * checking for e ...
- 五、python沉淀之路--字典
一. 1.根据序列,创建字典,并指定统一的值 v = dict.fromkeys(["],222) print(v) {': 222} 2.根据key 获取值,key不存在时,报错:get方 ...
- 深入理解Spring IOC
转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...
- python basestring()
作用: basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj, b ...
- BZOJ4198:[NOI2015]荷马史诗
浅谈\(Huffman\)树:https://www.cnblogs.com/AKMer/p/10300870.html 题目传送门:https://lydsy.com/JudgeOnline/pro ...
- 4、运行成功的Demo(PyCharm+Selenium)
1.打开PyCharm,新建一个python.file,输入代码“from selenium import webdriver”报错的解决方法 (1)PyCharm没有找到正确的python,在“Fi ...
- 本地dns服务器到底是什么?有没有精确的概念?
1.本地dns到底是什么?为什么有时候看到的本地dns的ip是局域网类型的ip? 有的人说本地dns的概念——————是运营商提供的dns, 有的人也说,是你的局域网里的路由器一类的设备里的dns. ...