find min and max -- 1.5n times for comparison
2014-03-06 16:01
369 查看
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;
}
}
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;
}
}
相关文章推荐
- C++ Find Min and Max element in a BST
- Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- Is there any way to define a min and max value for edittext in android?
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
- maxHeap & minHeap to find median
- Could not find action or result,There is no Action mapped for namespace / and action name hello_struts. - [unknown location]
- Setting Expires and Cache-Control: max-age headers for static resources in ASP.NET
- Min and Max
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- std::min and std::max 出错解决方案
- 3D for the Web : Interactive 3D animation using 3ds max, Flash and Director
- 9 Best File Comparison and Difference (Diff) Tools for Linux
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- [CSS] 详细解释 @media 属性与 (max-width:) and (min-width) 之间的关系及用法
- 关于Yuri Boykov and Vladimir Kolmogorov 于2004年提出的max flow / min cut的算法的详解
- std::min and std::max
- What is the difference between max-device-width and max-width for mobile web?