百度面试题:从已排序的数组中求绝对值最小的元素

本文探讨了如何在已排序的数组中找到绝对值最小的元素,并提供了一种高效的二分查找算法实现,该算法避免了顺序比较,复杂度低于O(n)。

题目为:

有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现

例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。

这一题该如何求呢?

初步的解决思路是:

    1.数组中的元素全为正,取最左边的数字;

    2.数组中的元素全为负,取最右边的数字的绝对值;

    3.数组中有正数有负数,就用二分法查找,判断中间元素的符号

       a)中间元素为正,继续判断中间元素前面一个元素的符号

       b)中间元素为负,判断中间元素后一个元素的符号

       c)中间元素为零,令其等于结果值返回

下面是根据上面的想法的代码实现,应该还会有漏洞

#include "stdafx.h"
#include <iostream>

using namespace std;

//求取数组中绝对值最小的数字
int minAbsolute(int arr[],int size);
//返回两个数中较小的数
int compare(int a,int b);
int _tmain(int argc, _TCHAR* argv[])
{
        int a[10] = {-10,-8,-5,-3,2,5,8,9,11,15};        
        int size = sizeof(a)/sizeof(int);
        int result = minAbsolute(a,size);
        cout<<"绝对值最小的数是:"<<result<<endl;
        return 0;
}

int minAbsolute(int arr[],int size)
{
        int first,last,mid;        
        first = 0;
        last = size - 1;
        int result;

        //数组中的数全是负数,取最右边的数
        if (arr[0] < 0 && arr[size-1] < 0)
        {
                result = arr[size-1];
        } 
        //数组中的数全是正数,取最左边的数
        else if (arr[0] > 0 && arr[size-1] > 0)
        {
                result = arr[0];
        }
        //数组有正有负,二分查找
        else
        {
                while(first < last)
                {
                        int mid = (first + last)/2;
                        if (arr[mid] > 0)
                        {
                                if (arr[mid - 1] > 0)
                                {
                                        last = mid - 1;
                                } 
                                else if(arr[mid - 1] < 0)
                                {
                                        result = compare(-arr[mid - 1],arr[mid]);
                                        break;
                                }
                                else
                                {
                                        result = arr[mid - 1];
                                        break;
                                }
                        }
                        else if (arr[mid] < 0)
                        {
                                if (arr[mid + 1] < 0)
                                {
                                        first = mid + 1;
                                } 
                                else if (arr[mid + 1] > 0)
                                {
                                        result = compare(-arr[mid],arr[mid+1]);
                                        break;
                                } 
                                else
                                {
                                        result = arr[mid + 1];
                                        break;
                                }                                
                        } 
                        else
                        {
                                result = arr[mid];
                                break;
                        }
                }
        }
        return result;
}

int compare(int a,int b)
{
        if (a > b)
        {
                return b;
        } 
        else
        {
                return a;
        }
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值