算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax
2016-04-20 18:37
459 查看
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 ; 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-005计测试算法
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理
- UVA_333_Recognizing Good ISBNs
- Django学习随手记(八)
- Django学习随手记(七)
- CodeForces 615E Hexagons
- history.back()与history.go(-1)
- Golang中的闭包
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
- GOF设计模式阅读笔记之行为模型(上)
- Django 异常解决
- go--Ubuntu下安装GO LANG,测试输入hello world
- 机器学习系列(8)_读《Nature》论文,看AlphaGo养成
- Windows下visual studio code搭建golang开发环境
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
- Django之站内搜索-Solr,Haystack
- ubuntu14.04 RockMongo的配置
- Django filter符合表示"不等于"
- Django笔记 富文本编辑器 课题