2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]
【本文链接】
http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html
【题目】
对于一个由N个整数组成的数组,需要比较多少次才能把最大和最小的数找出来呢?
【分析】
1. 遍历两次数组,分别找出最大值和最小值,需要进行 2N 次比较。
2. 将数组中的元素分组,按顺序将数组中相邻的两个数分在同一组,用Max和Min来存储最大值和最小值。同一组比较完之后,较小的数与当前的最小值比较,如该数小于当前最小值,更新Min;较大的数与当前的最大值比较,若该数大于当前最大值,更新Max。Max初始化为数组前两个数中较大值,Min初始化为数组前两个组中较小值。这种方法的比较次数是(N/2)*3=1.5N次。
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/*
version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/7/10 */ void getMinMax(int a, int b, int *min, int *max) void findArrMinMax(int a[], int size, int *min, int *max) int i, j; if(*tempmax > *max) // if array size is odd, then the last element a[size-1] is not contained in previous steps,so compare here |
3. 使用分治法,在N个数中求最小值Min和最大值Max,我们只需要求出前后N/2个数的Min和Max,然后取较小的Min和较大的Max即可。设比较的次数为T(n),那么T(n)=2T(n/2)+2,T(1)=0,T(2)=1,这种方法的比较次数是1.5N-2次。
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/*
version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/7/10 */ void findArrMinMax(int a[], int begin, int end, int *min, int *max) int maxL, maxR; |
【参考】
http://blog.csdn.net/caryaliu/article/details/8135898
http://www.cnblogs.com/lovell-liu/archive/2011/09/07/2169644.html
【本文链接】
http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html
2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]的更多相关文章
- Javascript获取数组中的最大值和最小值的方法汇总
比较数组中数值的大小是比较常见的操作,下面同本文给大家分享四种放哪广发获取数组中最大值和最小值,对此感兴趣的朋友一起学习吧 比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用 ...
- 如何使用Math对象快速计算数组中的最大值或最小值
Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...
- C#获取一个数组中的最大值、最小值、平均值
C#获取一个数组中的最大值.最小值.平均值 1.给出一个数组 ,,,,,-,,,,}; 2.数组Array自带方法 本身是直接可以调用Min(),Max(),Average()方法来求出 最小值.最大 ...
- Java求一个数组中的最大值和最小值
原创作品,转载请注明出处:https://www.cnblogs.com/sunshine5683/p/9927186.html 今天在工作中遇到对一个已知的一维数组取出其最大值和最小值,分别用于参与 ...
- PHP获取以为数组中的最大值和最小值
1.PHP获取一维数组中的最大值 <?php $a=array('1','3','55','99'); $pos = array_search(max($a), $a); echo $a[$po ...
- 数组中的最大值以及最小值的位置变换的问题(C++)
将一个5×5的数组中的最大值放到数组的中心位置 分析:遍历数组,找到最大的元素,然后将该元素与中心位置的元素交换位置 #include<iostream> #include <std ...
- js 取得数组中的最大值和最小值(含多维数组)
转自:http://www.dewen.org/q/433 方法一: var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math. ...
- 求一个number数组中的最大值和最小值的差
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- 整理:Javascript获取数组中的最大值和最小值的方法汇总
方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //最小值 Array.prototype.min = function ...
随机推荐
- HYSBZ1036 树链剖分
这题我建了2棵线段树,这样来处理 最值和和值,简单的题目. #include<queue> #include<stack> #include<cmath> #inc ...
- hdu1358 KMP
求循环节. #include<stdio.h> #include<string.h> #define maxn 1000010 int next[maxn]; char s[m ...
- hdu2444 判断二分图+最大匹配
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...
- 【POJ 2923】Relocation(状压DP+DP)
题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...
- config 写入
d代码: # __author__ = liukun # coding:utf-8 line = input("please input the config \n :") #提示 ...
- php复习
最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- HDU 1285 确定比赛名次
传送门 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 高德地图3D版的使用方法
坐标拾取器: http://lbs.amap.com/console/show/picker 其经纬度与LatLng()方法中的经纬度是对调的 SDK和实例下载: http://lbs.amap.co ...
- Practical Machine Learning For The Uninitiated
Practical Machine Learning For The Uninitiated Last fall when I took on ShippingEasy's machine learn ...