【本文链接】

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次。

 C++ Code 
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)
{
    // compare 1 time
    if(a > b)
    {
        *min = b;
        *max = a;
    }
    else
    {
        *min = a;
        *max = b;
    }
}

void findArrMinMax(int a[], int size, int *min, int *max)
{
    )
        return;
    )
    {
        *min = *max = a[];
        return;
    }
    // get min max of a[0] a[1]
], min, max);

int i, j;
    int *tempmin, *tempmax;
    )
    {
        // get min max of a[i] a[j]
        getMinMax(a[i], a[j], tempmin, tempmax);

if(*tempmax > *max)
            *max = *tempmax;
        if(*tempmin < *min)
            *min = *tempmin;
    }

// if array size is odd, then the last element a[size-1] is not contained in previous steps,so compare here
)
    {
        ] > *max)
            *max = a[size - ];
        ] < *min)
            *min = a[size - ];
    }
}

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次。

 C++ Code 
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)
{
    )
    {
        if(a[end] > a[begin])
        {
            *max = a[end];
            *min = a[begin];
        }
        else
        {
            *max = a[begin];
            *min = a[end];
        }
        return;
    }

int maxL, maxR;
    int minL, minR;
    ;
    findArrMinMax(a, begin, mid, &minL, &maxL);
    findArrMinMax(a, mid + , end, &minR, &maxR);
    *min = minL > minR ? minR : minL;
    *max = maxL > maxR ? 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]的更多相关文章

  1. Javascript获取数组中的最大值和最小值的方法汇总

    比较数组中数值的大小是比较常见的操作,下面同本文给大家分享四种放哪广发获取数组中最大值和最小值,对此感兴趣的朋友一起学习吧   比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用 ...

  2. 如何使用Math对象快速计算数组中的最大值或最小值

    Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...

  3. C#获取一个数组中的最大值、最小值、平均值

    C#获取一个数组中的最大值.最小值.平均值 1.给出一个数组 ,,,,,-,,,,}; 2.数组Array自带方法 本身是直接可以调用Min(),Max(),Average()方法来求出 最小值.最大 ...

  4. Java求一个数组中的最大值和最小值

    原创作品,转载请注明出处:https://www.cnblogs.com/sunshine5683/p/9927186.html 今天在工作中遇到对一个已知的一维数组取出其最大值和最小值,分别用于参与 ...

  5. PHP获取以为数组中的最大值和最小值

    1.PHP获取一维数组中的最大值 <?php $a=array('1','3','55','99'); $pos = array_search(max($a), $a); echo $a[$po ...

  6. 数组中的最大值以及最小值的位置变换的问题(C++)

    将一个5×5的数组中的最大值放到数组的中心位置 分析:遍历数组,找到最大的元素,然后将该元素与中心位置的元素交换位置 #include<iostream> #include <std ...

  7. js 取得数组中的最大值和最小值(含多维数组)

    转自:http://www.dewen.org/q/433 方法一: var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math. ...

  8. 求一个number数组中的最大值和最小值的差

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  9. 整理: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 ...

随机推荐

  1. Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before)

    一.不得不提的volatile volatile是个很老的关键字,几乎伴随着JDK的诞生而诞生,我们都知道这个关键字,但又不太清楚什么时候会使用它:我们在JDK及开源框架中随处可见这个关键字,但并发专 ...

  2. 配置Junit测试程序

    第一步:加载所需要的包:右键-->Build Path-->Configure Build Path-->Libraries-->Add Library-->Junit ...

  3. HDU 1285 确定比赛名次

    传送门 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. Longest Common Subsequence (LCS)

    最长公共子序列(LCS)是经典的DP问题,求序列a[1...n], b[1..m]的LCS. 状态是DP[i][j],表示a[1..i],b[1..j]的LCS. DP转移方程是 DP[i][j]= ...

  5. IbatisNet的介绍和使用

    http://dragonsuc.cnblogs.com/archive/2006/07/04/ibatisnet.html IbatisNet的介绍和使用http://files.cnblogs.c ...

  6. 初学Hibernate持久化

    hibernate三种持久化对象状态:(持久化对象:Persistent Object=POJO + hbm映射) 1.瞬时状态(临时状态或自由态):PO对象刚创建(即new)开始进入瞬时状态,此时对 ...

  7. Metropolis Light Transport学习与实现

    这段时间一直在看Metropolis Light Transport(简称mlt),现利用这篇博文把之前看资料已经coding上的一些体会记录下来. 1.Before MLT 在MLT算法被提出之前, ...

  8. linux ftp命令(转)

    此命令需要安装ftp, yum install ftp 1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 ...

  9. ftp (文件传输协议)

    ftp (文件传输协议) 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议” ...

  10. jQuery特效

    基础特效 方法 描述 hide() 立即隐藏jQuery对象内的所有元素 hide(time).hide(time, easing) 在指定的时间内以动画方式隐藏jQuery对象内的所有元素,并可选一 ...