Search in Sorted array,search in rotated Sorted array,search in rotated Sorted arrayii

Source: Internet
Author: User

One: Search in Sorted Array

Binary lookup, can have duplicate elements, return to the location of the target, just return to one of the locations, the code in the search range is [Low,high), left closed to the right, otherwise easily shone into a dead loop.

Code:

classSolution { Public:    intSearch (vector<int>& Nums,inttarget) {        intNumssize =nums.size (); intLow =0, high =numssize;  while(Low <High ) {            intMID = low + (high-low)/2; if(nums[mid]==target) {                returnmid; }Else if(nums[mid]<target) {low = mid+1;            }Else if(nums[mid]>target) {high = mid;            }        }        return-1; }};

Two: Search in rotated Sorted Array

Suppose a sorted array is rotated on some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).

You is given a target value to search. If found in the array is return its index, otherwise return-1.

Assume no duplicate exists in the array.

Duplicate element does not exist in array

There is no way to directly target the target at the midpoint element, as the usual two-point lookup, which needs to be discussed. If Target<a[mid], there are two possible, one is mid on the left ordered array, the other may be mid on the right of the ordered array. Mid is an ordered array on the left, then Target has two possibilities, either an ordered array on the left or an ordered array on the right, or, if mid is an ordered array on the right, only one possible, only in the right-hand array. The same can be discussed when Target>a[mid] is the case. And mid on the left ordered array or the right ordered array can be determined by the relationship of A[mid]>a[low]? Of course you can draw the analysis, the Red Line section indicates the possible location of mid:

Code:

classSolution { Public:    intSearch (vector<int>& Nums,inttarget) {        intNumssize =nums.size (); intLow =0, high =numssize;  while(Low <High ) {            intMID = low + (high-low)/2; if(nums[mid]==target) {                returnmid; }Else if(nums[mid]<target) {                if(Nums[mid]>nums[low]) {//mid in the left areaLow = mid+1; }Else{//mid in the right area                    if(target>Nums[low]) { High=mid; }Else if(target = =Nums[low]) {                        returnLow ; }Else{ Low= mid+1; }                }            }Else if(nums[mid]>target) {                if(Nums[mid]>nums[low]) {//mid is in the left area, Target has two possible regional locations                    if(target>Nums[low]) { High=mid; }Else if(target = =Nums[low]) {                        returnLow ; }Else{ Low= mid+1; }                }Else{//mid in the right areaHigh =mid; }            }        }        return-1; }};

Three: Search in rotated Sorted arrayii

Follow to "Search in rotated Sorted Array":
What if duplicates is allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given the target was in the array.

If there are duplicate elements in the array, the time complexity is degraded to O (n).

If there are duplicate elements, when A[mid]>=a[low], we cannot determine whether the mid-left ordered array or the right ordered array, can be drawn to understand, the following two pictures, the red line part of the position of the mid, the two images are A[mid]>=a[low]

Since when Target>a[mid], we cannot determine which mid is in the ordered array, so we can not discuss, at this time, we will low up one, high drop one.

Code:

classSolution { Public:    BOOLSearch (vector<int>& Nums,inttarget) {        intNumssize =nums.size (); intLow =0, high =numssize;  while(Low <High ) {            intMID = low + (high-low)/2; if(nums[mid]==target) {                return true; }Else if(nums[mid]<target) {                if(Nums[mid]>=nums[low]) {//unable to determine if mid is in the left or right area                    if(target = = Nums[low] | | target==nums[high-1]){                        return true; } Low++; High--; }Else{//mid in the right area                    if(target>Nums[low]) { High=mid; }Else if(target = =Nums[low]) {                        return true; }Else{ Low= mid+1; }                }            }Else if(nums[mid]>target) {                if(Nums[mid]>=nums[low]) {//unable to determine if mid is in the left or right area                    if(target = = Nums[low] | | target==nums[high-1]){                        return true; } Low++; High--; }Else{//mid in the right areaHigh =mid; }            }        }        return false; }};

Search in Sorted array,search in rotated Sorted array,search in rotated Sorted arrayii

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.