find min and max -- 1.5n times for comparison
来源:互联网 发布:广联达软件有哪些 编辑:程序博客网 时间:2024/04/30 09:30
1. divide and conquer
public static int[] findMinMax(int[] array, int[] res, int start , int end){int[] minmax = new int[2];if(end - start <= 1){if(array[start] < array[end]){minmax[0] = array[start] ;minmax[1] = array[end] ;}else {minmax[1] = array[start] ;minmax[0] = array[end] ;}return minmax;}int mid = (start + end )/2;int[] leftminmax = findMinMax(array, res, start, mid);int[] rightminmax = findMinMax(array, res, mid+1, end);if(leftminmax[0] < rightminmax[0]){minmax[0] = leftminmax[0];}else {minmax[0] = rightminmax[0];}if(leftminmax[1] > rightminmax[1]){minmax[1] = leftminmax[1];}else {minmax[1] = rightminmax[1];}return minmax;}
2. adjacent comparison
public static void findMinMax(int[] array){int[] minmax = new int[2];minmax[0] = array[array.length-1];minmax[1] = array[array.length-1];int min = Integer.MAX_VALUE;int max = Integer.MIN_VALUE;for(int i =0; i<array.length/2*2; i=i+2){if(array[i] > array[i+1]){min = array[i+1];max = array[i];}else{min = array[i];max = array[i+1];}if(min < minmax[0] )minmax[0] = min;if(max > minmax[1] )minmax[1] = max;}}
0 0
- find min and max -- 1.5n times for comparison
- C++ Find Min and Max element in a BST
- checkio min and max
- Calculating Max and Min
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- Min-Max Max-Min problem algorithm and analysis
- Is there any way to define a min and max value for edittext in android?
- compute the MAX and MIN of int
- Min and Max Timing Paths--时序路径
- std::min and std::max 出错解决方案
- jquery get min and get max
- Python checkio Min and Max解决方案
- Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
- Html5游戏开发攻略(像素效果篇)
- JS 网页快捷键设置
- centos 下安装 python 模块
- 瓉圩眄早上早蝇蝇嗢国一犄上
- 我的第十四课:jQuery - 添加元素
- find min and max -- 1.5n times for comparison
- 虚拟机下Ubuntu没有GUI图形界面,解决方法
- js类式继承的实现
- 轻松在mac上配置svn服务器
- 【C语言】17-预处理指令3-文件包含
- Activity——简介和使用
- Linux namespace
- iphone开发3G,wifi网络的切换
- "singleTask"启动模式的Activity的特点