码迷,mamicode.com
首页 > 其他好文 > 详细

来说说二分查找

时间:2015-04-01 17:46:37      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:c++   二分查找   算法   

二分查找是我们经常会遇到的算法,思路清晰,代码简洁。二分查找要求序列有序,且支持随机存取,一般情况下我们讨论的序列不存在相同元素,则二分查找可以很熟练的表示如下:

int binsearch(int A[], int n, int target)
    {
        int left=0,right=n-1,res=-1;
        while(left<=right)
        {
            int mid = left+((right-left)>>1);
            if(A[mid]<target)
                left = mid+1;
            else if(A[mid]>target)
                right = mid-1;
            else
            {
                return mid;
            }
        }
        return -1;
    }
但是当我们对有序序列不做要求时,即可能出现相同的元素的情况下,二分查找就回出现一些扩展的问题,比如:

  1. 返回key的第一次出现的下标,若没有返回-1;
  2. 返回key最后一次出现的下标,若没有返回-1;
  3. 返回刚好小于key的元素的下标,若没有返回-1;
  4. 返回刚好大于key的元素的下标,若没有返回-1; 
对于(1)而言,当查找到与key相同的元素时,并不马上返回,而是先记录下来,然后继续查找左边是否还存在与key相同的元素。
int searchLowerBound(int A[], int n, int target)
    {
        int left=0,right=n-1,res=-1;
        while(left<=right)
        {
            int mid = left+((right-left)>>1);
            if(A[mid]<target)
                left = mid+1;
            else if(A[mid]>target)
                right = mid-1;
            else
            {
                res = mid;
                right = mid-1;
            }
        }
        return res;
    }
对于(2),同理可得:
int searchHigherBound(int A[], int n, int target)
    {
        int left=0,right=n-1,res=-1;
        while(left<=right)
        {
            int mid = left+((right-left)>>1);
            if(A[mid]<target)
                left = mid+1;
            else if(A[mid]>target)
                right = mid-1;
            else
            {
                res = mid;
                left = mid+1;
            }
        }
        return res;
    }
对于(3),使得key=key-1,则转换成问题(2),对于(4),使得key=key+1,则转换成问题(1)。

下面我以一个leetcode中出现的二分查找的题目为例:search for a range
题目描述:

Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm‘s runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

这个题就是要求出最小的value的下标和最大的value的下标,所以代码易得:

class Solution {
public:
    int searchLowerBound(int A[], int n, int target)
    {
        int left=0,right=n-1,res=-1;
        while(left<=right)
        {
            int mid = left+((right-left)>>1);
            if(A[mid]<target)
                left = mid+1;
            else if(A[mid]>target)
                right = mid-1;
            else
            {
                res = mid;
                right = mid-1;
            }
        }
        return res;
    }
    
     int searchHigherBound(int A[], int n, int target)
    {
        int left=0,right=n-1,res=-1;
        while(left<=right)
        {
            int mid = left+((right-left)>>1);
            if(A[mid]<target)
                left = mid+1;
            else if(A[mid]>target)
                right = mid-1;
            else
            {
                res = mid;
                left = mid+1;
            }
        }
        return res;
    }
    
    vector<int> searchRange(int A[], int n, int target) {
        vector<int> res;
        res.push_back(searchLowerBound(A,n,target));
        res.push_back(searchHigherBound(A,n,target));
        return res;
    }
};

来说说二分查找

标签:c++   二分查找   算法   

原文地址:http://blog.csdn.net/majing19921103/article/details/44808663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!