同时找出在数组中的最大值、最小值
2013-12-05 00:04
288 查看
If n is odd then initialize min and max as first element.
If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
For rest of the elements, pick them in pairs and compare their
maximum and minimum with max and min respectively.
Time Complexity: O(n)
Total number of comparisons: Different for even and odd n, see below:
http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/
编程之美
If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
For rest of the elements, pick them in pairs and compare their
maximum and minimum with max and min respectively.
Total number of comparisons: Different for even and odd n, see below:
If n is odd: 3*(n-1)/2 If n is even: 1 Initial comparison for initializing min and max, and 3(n-2)/2 comparisons for rest of the elements = 1 + 3*(n-2)/2 = 3n/2 -2
http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/
编程之美
相关文章推荐
- 从一个数组中同时找出最大最小数-算法导论第九章
- 死磕==同时找出数组中得最大值和最小值
- 同时找出数组的最大值和最小值
- 同时找出数组的最大值和最小值,要求时间复杂度为o(n)
- 找出数组中最大最小数(用指针)
- 通过随机数生成办法,向int num[10][10]的数组 中存入数据并找出数组的最大、最小值及其下标。
- 无序数组同时查找最大和最小的元素
- 实验9 指针1 1、程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依据。
- 同时找数组里的最大最小数——分治思想
- 实验9(1)程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依据。
- 通过3(2/n)次比较同时找到数组最大和最小值(python)
- 【十一】找出一组数组中最大值、最大值的角标、最小值、最小值的角标及平均数
- 算法8:一个整数数组里怎么同时找最大和最小的数,尽量优化比较次数
- 找出整型数组中最大和最小值及其所在位置
- 同时找出最大值和最小值
- 笔试题&面试题:给定n个数,要求比较次数1.5n同时找出最大值和最小值
- 从数组中同时取出最大最小元素
- 【算法导论】同时求数组的最大值和最小值
- 在规定次数内找出数组中最大值和最小值
- 同时寻找一个数组中的最大元素和最小元素--你会有所收获