查找数组中第二大的数值

本文介绍了一种简单有效的算法来找到整数数组中的第二大值,并考虑了多种边界情况,包括数组只有一个元素、所有元素相同等情况。同时提供了一个查找数组第二小元素的方法。
题目:写一个函数找出一个整数数组中,第二大的数。【Mirosoft

解答:
None.gifint FindSecondMaxValue(int src[], int count)
ExpandedBlockStart.gif{
InBlock.gif    int max = 0;
InBlock.gif    int secondMax = 0;
InBlock.gif
InBlock.gif    if (count==0) return secondMax;
InBlock.gif    if (count==1)
ExpandedSubBlockStart.gif    {
InBlock.gif        return src[0];
ExpandedSubBlockEnd.gif    }
InBlock.gif    else if (src[0] > src[1])
ExpandedSubBlockStart.gif    {
InBlock.gif        max = src[0];
InBlock.gif        secondMax = src[1];
ExpandedSubBlockEnd.gif    }
InBlock.gif    else
ExpandedSubBlockStart.gif    {
InBlock.gif        max = src[1];
InBlock.gif        secondMax = src[0];
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    for (int i=2; i<count; ++i)
ExpandedSubBlockStart.gif    {
InBlock.gif        if (src[i] >= max)
ExpandedSubBlockStart.gif        {
InBlock.gif            secondMax = max;
InBlock.gif            max = src[i];
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            if (src[i]>secondMax)
ExpandedSubBlockStart.gif            {
InBlock.gif                secondMax = src[i];
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    return secondMax;
ExpandedBlockEnd.gif}

算法本身是简单的,但是一些边界条件需要注意:
1.数组的元素数量为1,0个;
2.数组所有元素的数值相等;
3.数组元素只有2个不同的数值。

以上代码还不是很健壮,不过基本逻辑应该是OK的,以下是测试代码,测试了相关的边界条件。

None.gifvoid testFindSecondMaxValue()
ExpandedBlockStart.gif{
InBlock.gif    const int array_size = 10;
InBlock.gif
InBlock.gif    // 一般情况
ExpandedSubBlockStart.gif
    int arr1[array_size]={0, -1, 1, 87, 3354, 24, -56, 355, 687, -100};
InBlock.gif    std::cout << "数组中第二大数为:" << FindSecondMaxValue(arr1, array_size ) << std::endl;
InBlock.gif
InBlock.gif    // 数组元素只有2个不同的数值
ExpandedSubBlockStart.gif
    int arr2[array_size]={0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
InBlock.gif    std::cout << "数组中第二大数为:" << FindSecondMaxValue(arr2, array_size ) << std::endl;
InBlock.gif
InBlock.gif    // 数组所有元素的数值相等
ExpandedSubBlockStart.gif
    int arr3[array_size]={1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
InBlock.gif    std::cout << "数组中第二大数为:" << FindSecondMaxValue(arr3, array_size ) << std::endl;
InBlock.gif
InBlock.gif    // 只有0个元素的数组
InBlock.gif    
//int arr4[0];
InBlock.gif    
//std::cout << "数组中第二大数为:" << FindSecondMaxValue(arr4, 0 ) << std::endl;
InBlock.gif
InBlock.gif    
// 只有1个元素的数组
ExpandedSubBlockStart.gif
    int arr5[1]={1};
InBlock.gif    std::cout << "数组中第二大数为:" << FindSecondMaxValue(arr5, 1 ) << std::endl;
InBlock.gif
ExpandedBlockEnd.gif}
不过0数组在VS2005里面已经被禁止掉了,所以arr4编译是会要报错的。


附送一个求数组第二小的元素的查找算法:
None.gifint FindSecondMinValue(int src[], int count)
ExpandedBlockStart.gif{
InBlock.gif    int min = 0;
InBlock.gif    int secondMin = 0;
InBlock.gif
InBlock.gif    if (count==0) return secondMin;
InBlock.gif    if (count==1)
ExpandedSubBlockStart.gif    {
InBlock.gif        return src[0];
ExpandedSubBlockEnd.gif    }
InBlock.gif    else if (src[0] < src[1])
ExpandedSubBlockStart.gif    {
InBlock.gif        min = src[0];
InBlock.gif        secondMin = src[1];
ExpandedSubBlockEnd.gif    }
InBlock.gif    else
ExpandedSubBlockStart.gif    {
InBlock.gif        min = src[1];
InBlock.gif        secondMin = src[0];
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    for (int i=2; i<count; ++i)
ExpandedSubBlockStart.gif    {
InBlock.gif        if ( src[i]<=min )
ExpandedSubBlockStart.gif        {
InBlock.gif            secondMin = min;
InBlock.gif            min = src[i];
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            if ( src[i] < secondMin)
InBlock.gif                secondMin = src[i];
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    return secondMin;
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值