您的位置:首页 > 其它

同时找出在数组中的最大值、最小值

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:

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/

编程之美
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐