To find a number in an ordered array of rotation (algorithm)

Source: Internet
Author: User

Topic:

Suppose an ordered array rotates at a certain position, and a new array is rotated, which is an ordered array of rotations. such as: (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).

Now given an array of these, look for a number in the array. If found, returns the subscript, otherwise returns-1;

Ideas:

Idea 1:

Consider the characteristics of an ordered array of rotations: The front part is incremented, and the latter part is incremented, that is, the two parts are incrementing an ordered array, so you can use a binary search to do it.

Suppose that the array is a, the subscript is from left to right, and the number to find is target, then mid= (left+ ((right-left) >>1))

If A[mid]==target, return mid;

What if A[mid]>target or A[mid]<target? In mid, the left and right sides are not necessarily an ordered array. At this point we don't compare the size of A[mid] and target.

In mid, the left and right sides must have an ordered array of increments on one side, so this is the interval we're looking for.

If A[mid]>a[left], then the left part of mid is an ascending ordered array, if TARGET>=A[LEFT] && Target<a[mid], then right=mid-1; otherwise, left=mid +1;

If A[mid]<a[left], then the right part of mid is an ascending ordered array, if TARGET>A[MID] && target<=a[right], then left=mid+1; otherwise, right= Mid-1;

Idea 2:

Of course, we can also think from the normal thinking, from the A[mid]==target,a[mid]>target,a[mid]<target three parts to consider the boundary conditions. (See Code)

Code:
#include <iostream>using namespacestd;intRotatearraysearch_1 (int*a,intLeninttarget) {    intleft=0; intright=len-1;  while(left<=Right ) {        intmid=left+ (Right-left) >>1); if(a[mid]==target)returnmid; if(a[mid]>A[left]) {            if(Target>=a[left] && target<A[mid]) right=mid-1; Else Left=mid+1; }        Else{            if(Target>a[mid] && target<=A[right]) left=mid+1; Else Right=mid-1; }    }    return-1;}intRotatearraysearch_2 (int*a,intLeninttarget) {    intleft=0; intright=len-1;  while(left<=Right ) {        intmid=left+ (Right-left) >>1); if(a[mid]==target)returnmid; Else if(target>A[mid]) {            if(A[mid]>a[left] | | target<=A[right]) left=mid+1; Else Right=mid-1; }        Else{            if(A[mid]<a[right] | | target>=A[left]) right=mid-1; Else Left=mid+1; }    }    return-1;}intMain () {inta[]={2,3,4,5,1}; intb[]={5,1,2,3,4}; intn=sizeof(A)/sizeof(a[0]); cout<< rotatearraysearch_1 (A,n,5) <<Endl; cout<< rotatearraysearch_1 (A,n,3) <<Endl; cout<< rotatearraysearch_2 (B,n,1) <<Endl; cout<< rotatearraysearch_2 (B,n,4) <<Endl; return 0;}

To find a number in an ordered array of rotation (algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.