Loop ordered array of binary search Search in a rotated sorted array


Binary search is necessary to master the skills for an orderly, sequential storage structure.

1, can use it to locate a number of

2, it can be used to find a certain range. Such as "Find the number of ordered arrays in a range where the dichotomy Search for a Range".

3, you can use it to find the median of two ordered arrays.

4, in this article, binary search addition of a new skill, you can use it to look up a number in the loop ordered array.

After two min to find the key, wherein the intermediate index is determined mid, I could tell which side of the key mid, and then to find that side.

Loop ordered array:

Means that an ordered array of circular left / right after several distance into the array. E.g., [1,2,3,4,5] Rotate Right 3, becomes [4,5,1,2,3]. Features of the array is that which contains a turning point. A turning point in the left and right sides of the sub-arrays are ordered, and the right subarray of the overall array are larger than the left child.

Find Method:

Do not think the turning point positioned directly.

Just need to know which side of the turning point in the mid-point on the line. Because the side without turning point must be monotonically increasing, it is still able to help me determine which side of the key in the mid.

The left and right subscripts, subscript mid determined.

If A [left] <= A [mid], then A [mid] is not necessarily a turning point in the left side, from left to mean the entire left half of the mid is strictly increasing, and therefore I was able to determine whether the key in the left half in.

If A [left]> A [mid], then A [mid] one o'clock in the left turning point, means that from mid to the entire right half of the right is strictly increasing, and therefore I was able to determine whether the key was in the right half.

Implement (not contain duplicate elements in an array) a:


class Solution {
public:
 
int search(int A[], int n, int target)
{
    if(n<=0)
        return -1;
    int left = 0, right = n-1;
    while(left<=right)
    {
        int mid = left + ((right-left)/2);
        if(A[mid] == target)
            return mid;
 
        if(A[left] <= A[mid])//转折点在右半边
        {
            if(A[left] <= target && target < A[mid])
                right = mid - 1;
            else
                left = mid + 1;
        }
        else //转折点在左半边
        {
            if(A[mid] < target && target <= A[right])
                left = mid + 1;
            else
                right = mid - 1;
        }
    }
    return -1;
}
 
};


The above method does not consider the case where there is an array of repeating elements.

Implement two (possible duplication of elements in the array):


class Solution {
    public:
        bool search(int A[], int n, int target) {
            return bisearch(A, 0, n-1, target);
        }
 
        bool bisearch(int A[], int left, int right, int target)
        {
            if(left > right)
                return false;
            int mid = left + (right - left)/2;
            if(target == A[mid])
                return true;
            // A[left], A[mid], A[right]
            // 1 3 5
            if(A[mid] > A[left] && A[mid] < A[right]) //普通二分
            {
                if(target < A[mid])
                    return bisearch(A, left, mid-1, target);
                else
                    return bisearch(A, mid+1, right, target);
            }
            // 5 1 3
            else if(A[mid] < A[left] && A[mid] < A[right]) //转折在左侧
            {
                if(target > A[mid] && target <= A[right])
                    return bisearch(A, mid+1, right, target);
                else
                    return bisearch(A, left, mid-1, target);
            }
            // 3 5 1
            else if(A[mid] > A[left] && A[mid] > A[right]) //转折在右侧
            {
                if(target < A[mid] && target >= A[left])
                    return bisearch(A, left, mid-1, target);
                the else
                    return bisearch (A, + MID. 1, right, target);
            }
            //. 3. 3. 3
            the else // no way to be in this case are equal to the left, right judgment
            {
                return bisearch (A, left,. 1-MID, target) || bisearch (A, + MID. 1, right, target);
            }
 
        }
 
 
};

 

Published 17 original articles · won praise 2 · views 50000 +

Guess you like

Origin blog.csdn.net/u011250186/article/details/103865170