Search in rotated sorted array I

Source: Internet
Author: User

The object to be searched is a rotated sorted array, so the time complexity intuitively should not exceed O (logn ). At first I tried to modify binary search to solve this problem, but after careful consideration, it was difficult to determine the boundary of search in the process of continuous search. Another way to solve this problem is to first find out the limit. Here I define the limit as the smallest number in the array. So the solution below is to find the shard at O (logn) time. Following this idea and then combining the features of the rotated sorted array, we can use a method similar to binary serach to find the rotated sorted array. Attention has the following features: the numbers on the left and right of the weight are greater than that on the weight itself. The determination of the search direction can be determined by comparing the number of current searches with a [0]. If a [current]> = A [0] indicates that the cursor is in [current, n] is in the range, and vice versa is in the range of [0, current-1. However, note that there is no limit, that is, the array is sorted but not rotated.

After obtaining the secondary node, we can search for the target using binary search in the two regions of the secondary node. The time complexity is O (logn ).

 1 class Solution { 2 public: 3     int search(int A[], int n, int target) { 4         int pivot = search_pivot(A,0,n-1); 5         if(pivot == -1) return binary_search(A,0,n-1,target); 6         if(target > A[pivot-1] || target < A[pivot] || target < A[0] && target > A[n-1]) return -1; 7         if(target >= A[0]) return binary_search(A,0,pivot-1,target); 8         else return binary_search(A,pivot,n-1,target); 9     }10     int search_pivot(int A[], int left, int right){11         if(left >= right) return -1;12         int mid = (left + right)/2;13         if(A[mid] < A[0]){14             if(A[mid-1]>A[mid]) return mid;15             else  return search_pivot(A, left, mid-1);16         }else {17             if(A[mid+1]<A[mid]) return mid+1;18             else return search_pivot(A,mid + 1,right);19         }20     }21     int binary_search(int A[], int left, int right, int target){22         if(left > right) return -1;23         int mid = (left + right)/2;24         if(A[mid] == target) return mid;25         if(A[mid] > target) return binary_search(A,left,mid-1,target);26         if(A[mid] < target) return binary_search(A,mid+1, right,target);27     }28 };

 

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.