【本文链接】

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. LinkedHashMap实现LRU算法

    LinkedHashMap特别有意思,它不仅仅是在HashMap上增加Entry的双向链接,它更能借助此特性实现保证Iterator迭代按照插入顺序(以insert模式创建LinkedHashMap) ...

  2. [原]Golang FileServer

    转载请注明出处 今天我们用go来搭建一个文件服务器FileServer,并且我们简单分析一下,它究竟是如何工作的.知其然,并知其所以然! 首先搭建一个最简单的,资源就挂载在服务器的根目录下,并且路由路 ...

  3. BZOJ2460 [BeiJing2011]元素

    Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力越 ...

  4. BZOJ1016 最小生成树计数

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  5. c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常

    #include"stdafx.h" #include"string" #include<iostream> #include<vector& ...

  6. C++ 中常见预定义宏的使用

    http://blog.csdn.net/hgl868/article/details/7058906 替代字符串: #define DOWNLOAD_IMAGE_LOG /var/log/png.l ...

  7. IE 兼容模式下不支持DIV CSS样式display:inline-block,解决

    样式改为: display: inline-block;*display: inline;zoom: 1; 就可以了

  8. mlecms v2.2版权

    inc\tools\smarty 下的Smarty.class.php文件. 找到 187行左右 我们会发现原来的  $dopud =$_template->libfile($dopud);已经 ...

  9. java连接各种数据库代码大全

    1.Oracle8/8i/9i数据库(thin模式)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();S ...

  10. Linux(Ubuntu)下面SecureCRT 完全破解

    转载声明:本文来自http://www.boll.me/archives/680 相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确 ...